Checkpoint synchronization: Difference between revisions

Added PicoLisp
(J draft)
(Added PicoLisp)
Line 474:
Worker 2 is ready
</pre>
 
=={{header|PicoLisp}}==
The following solution implements each worker as a coroutine. Therefore, it
works only in the 64-bit version.
 
'checkpoints' takes a number of projects to do, and a number of workers. Each
worker is started with a random number of steps to do (between 2 and 5), and is
kept in a list of 'Staff' members. Whenever a worker finishes, he is removed
from that list, until it is empty and the project is done.
 
'worker' takes a number of steps to perform. It "works" by printing each step,
and returning NIL when done.
<lang PicoLisp>(de checkpoints (Projects Workers)
(for P Projects
(prinl "Starting project number " P ":")
(for
(Staff
(mapcar
'((I) (worker (format I) (rand 2 5))) # Create staff of workers
(range 1 Workers) )
Staff # While still busy
(filter '((ID) (worker ID)) Staff) ) ) # Remove finished workers
(prinl "Project number " P " is done.") ) )
 
(de worker (ID Steps)
(co ID
(prinl "Worker " ID " has " Steps " steps to do")
(for N Steps
(yield ID)
(prinl "Worker " ID " step " N) )
NIL ) )</lang>
Output:
<pre>: (checkpoints 2 3) # Start two projects with 3 workers
Starting project number 1:
Worker 1 has 2 steps to do
Worker 2 has 3 steps to do
Worker 3 has 5 steps to do
Worker 1 step 1
Worker 2 step 1
Worker 3 step 1
Worker 1 step 2
Worker 2 step 2
Worker 3 step 2
Worker 2 step 3
Worker 3 step 3
Worker 3 step 4
Worker 3 step 5
Project number 1 is done.
Starting project number 2:
Worker 1 has 4 steps to do
Worker 2 has 3 steps to do
Worker 3 has 2 steps to do
Worker 1 step 1
Worker 2 step 1
Worker 3 step 1
Worker 1 step 2
Worker 2 step 2
Worker 3 step 2
Worker 1 step 3
Worker 2 step 3
Worker 1 step 4
Project number 2 is done.</pre>
 
=={{header|PureBasic}}==
Anonymous user