September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,38 +1,20 @@
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
#include <unistd.h>
using namespace std;
int main(int argc, char* argv[]) {
std::vector<std::thread> threads;
void sortThread( int x )
{
usleep( 10000 * x );
cout << x << " ";
}
int main()
{
vector<thread*> threads;
srand( ( unsigned )time( NULL ) );
cout << "unsorted:" << endl;
for( int x = 0; x < 15; x++ )
{
int r = rand() % 20 + 5;
cout << r << " ";
thread* t = new thread( sortThread, r );
threads.push_back( t );
}
cout << endl << endl << "sorted:" << endl;
for( vector<thread*>::iterator t = threads.begin(); t != threads.end(); t++ )
{
( *t )->join();
delete ( *t );
}
cout << endl << endl;
return 0;
for (int i = 1; i < argc; ++i) {
threads.emplace_back([i, &argv]() {
int arg = std::stoi(argv[i]);
std::this_thread::sleep_for(std::chrono::seconds(arg));
std::cout << argv[i] << std::endl;
});
}
for (auto& thread : threads) {
thread.join();
}
}

View file

@ -0,0 +1,3 @@
USING: threads calendar concurrency.combinators ;
: sleep-sort ( seq -- ) [ dup seconds sleep . ] parallel-each ;

View file

@ -0,0 +1 @@
{ 1 9 2 6 3 4 5 8 7 0 } sleep-sort

View file

@ -0,0 +1,33 @@
program sleepSort
use omp_lib
implicit none
integer::nArgs,myid,i,stat
integer,allocatable::intArg(:)
character(len=5)::arg
!$omp master
nArgs=command_argument_count()
if(nArgs==0)stop ' : No argument is given !'
allocate(intArg(nArgs))
do i=1,nArgs
call get_command_argument(i, arg)
read(arg,'(I5)',iostat=stat)intArg(i)
if(intArg(i)<0 .or. stat/=0) stop&
&' :Only 0 or positive integer allowed !'
end do
call omp_set_num_threads(nArgs)
!$omp end master
!$omp parallel private(myid)
myid =omp_get_thread_num()
call sleepNprint(intArg(myid+1))
!$omp end parallel
contains
subroutine sleepNprint(nSeconds)
integer::nSeconds
call sleep(nSeconds)
print*,nSeconds
end subroutine sleepNprint
end program sleepSort

View file

@ -4,9 +4,9 @@ import Control.Monad
sleepSort :: [Int] -> IO ()
sleepSort values = do
chan <- newChan
forM_ values (\time -> forkIO (threadDelay (50000 * time) >> writeChan chan time))
forM_ values (const (readChan chan >>= print))
chan <- newChan
forM_ values (\time -> forkIO (threadDelay (50000 * time) >> writeChan chan time))
forM_ values (\_ -> readChan chan >>= print)
main :: IO ()
main = getArgs >>= sleepSort . map read

View file

@ -2,8 +2,8 @@ import System.Environment
import Control.Concurrent
import Control.Concurrent.Async
sleepSort :: [Int] -> IO [()]
sleepSort = mapConcurrently (\x -> threadDelay (x*10^4) >> print x)
sleepSort :: [Int] -> IO ()
sleepSort = (() <$) . mapConcurrently (\x -> threadDelay (x*10^4) >> print x)
main :: IO [()]
main :: IO ()
main = getArgs >>= sleepSort . map read

View file

@ -0,0 +1,17 @@
object SleepSort {
def main(args: Array[String]): Unit = {
val nums = args.map(_.toInt)
sort(nums)
Thread.sleep(nums.max * 21) // Keep the JVM alive for the example
}
def sort(nums: Seq[Int]): Unit =
nums.foreach(i => new Thread {
override def run() {
Thread.sleep(i * 20) // just `i` is unpredictable with small numbers
print(s"$i ")
}
}.start())
}

View file

@ -0,0 +1,2 @@
$ scala SleepSort 1 3 6 0 9 7 4 2 5 8
0 1 2 3 4 5 5 6 7 8 9

View file

@ -1,12 +0,0 @@
object SleepSort {
def sort(nums:Seq[Int])=nums foreach {n =>
scala.concurrent.ops.spawn{
Thread.sleep(500*n)
print(n+" ")
}
}
def main(args:Array[String])={
sort(args map (_.toInt))
}
}

View file

@ -0,0 +1,20 @@
SIMULATION
BEGIN
PROCESS CLASS SORTITEM(N); INTEGER N;
BEGIN
HOLD(N);
OUTINT(N, 3);
END;
INTEGER I;
FOR I := 3, 2, 4, 7, 3, 6, 9, 1 DO
BEGIN
REF(SORTITEM) SI;
SI :- NEW SORTITEM(I);
ACTIVATE SI;
END;
HOLD(100000);
OUTIMAGE;
END;

View file

@ -0,0 +1,2 @@
vm.arglist.apply(fcn(n){ Atomic.sleep(n); print(n) }.launch);
Atomic.waitFor(fcn{ vm.numThreads == 1 }); Atomic.sleep(2); println();