Checkpoint synchronization: Difference between revisions

m
m (→‎{{header|Raku}}: Fix up Perl 6 -> Raku)
m (→‎{{header|Wren}}: Minor tidy)
 
(19 intermediate revisions by 8 users not shown)
Line 1:
{{task|Concurrency}}[[Category:Classic CS problems and programs]]{{requires|Concurrency}}
{{task|Concurrency}}{{requires|Concurrency}}
The checkpoint synchronization is a problem of synchronizing multiple [[task]]s. Consider a workshop where several workers ([[task]]s) assembly details of some mechanism. When each of them completes his work they put the details together. There is no store, so a worker who finished its part first must wait for others before starting another one. Putting details together is the ''checkpoint'' at which [[task]]s synchronize themselves before going their paths apart.
 
Line 11 ⟶ 12:
 
If you can, implement workers joining and leaving.
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Calendar; use Ada.Calendar;
with Ada.Numerics.Float_Random;
with Ada.Text_IO; use Ada.Text_IO;
Line 115:
end Test_Checkpoint;
 
</syntaxhighlight>
</lang>
Sample output:
<pre style="height: 200px;overflow:scroll">
Line 187:
D ends shift
</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"TIMERLIB"
nWorkers% = 3
DIM tID%(nWorkers%)
Line 248 ⟶ 247:
PROC_killtimer(tID%(I%))
NEXT
ENDPROC</langsyntaxhighlight>
'''Output:'''
<pre>
Line 269 ⟶ 268:
Worker 2 starting (5 ticks)
</pre>
 
=={{header|C}}==
Using OpenMP. Compiled with <code>gcc -Wall -fopenmp</code>.
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Line 302 ⟶ 300:
 
return 0;
}</langsyntaxhighlight>
=={{header|C sharp|C#}}==
{{works with|C sharp|10}}
<syntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
 
namespace Rosetta.CheckPointSync;
 
public class Program
{
public async Task Main()
{
RobotBuilder robotBuilder = new RobotBuilder();
Task work = robotBuilder.BuildRobots(
"Optimus Prime", "R. Giskard Reventlov", "Data", "Marvin",
"Bender", "Number Six", "C3-PO", "Dolores");
await work;
}
 
public class RobotBuilder
{
static readonly string[] parts = { "Head", "Torso", "Left arm", "Right arm", "Left leg", "Right leg" };
static readonly Random rng = new Random();
static readonly object key = new object();
 
public Task BuildRobots(params string[] robots)
{
int r = 0;
Barrier checkpoint = new Barrier(parts.Length, b => {
Console.WriteLine($"{robots[r]} assembled. Hello, {robots[r]}!");
Console.WriteLine();
r++;
});
var tasks = parts.Select(part => BuildPart(checkpoint, part, robots)).ToArray();
return Task.WhenAll(tasks);
}
 
private static int GetTime()
{
//Random is not threadsafe, so we'll use a lock.
//There are better ways, but that's out of scope for this exercise.
lock (key) {
return rng.Next(100, 1000);
}
}
 
private async Task BuildPart(Barrier barrier, string part, string[] robots)
{
foreach (var robot in robots) {
int time = GetTime();
Console.WriteLine($"Constructing {part} for {robot}. This will take {time}ms.");
await Task.Delay(time);
Console.WriteLine($"{part} for {robot} finished.");
barrier.SignalAndWait();
}
}
 
}
}</syntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Constructing Head for Optimus Prime. This will take 607ms.
Constructing Torso for Optimus Prime. This will take 997ms.
Constructing Left arm for Optimus Prime. This will take 201ms.
Constructing Right arm for Optimus Prime. This will take 993ms.
Constructing Left leg for Optimus Prime. This will take 165ms.
Constructing Right leg for Optimus Prime. This will take 132ms.
Right leg for Optimus Prime finished.
Left leg for Optimus Prime finished.
Left arm for Optimus Prime finished.
Head for Optimus Prime finished.
Right arm for Optimus Prime finished.
Torso for Optimus Prime finished.
Optimus Prime assembled. Hello, Optimus Prime!
 
Constructing Right arm for R. Giskard Reventlov. This will take 772ms.
Constructing Left leg for R. Giskard Reventlov. This will take 722ms.
Constructing Head for R. Giskard Reventlov. This will take 140ms.
Constructing Left arm for R. Giskard Reventlov. This will take 299ms.
Constructing Right leg for R. Giskard Reventlov. This will take 637ms.
Constructing Torso for R. Giskard Reventlov. This will take 249ms.
Head for R. Giskard Reventlov finished.
Torso for R. Giskard Reventlov finished.
Left arm for R. Giskard Reventlov finished.
Right leg for R. Giskard Reventlov finished.
Left leg for R. Giskard Reventlov finished.
Right arm for R. Giskard Reventlov finished.
R. Giskard Reventlov assembled. Hello, R. Giskard Reventlov!
 
//etc
</pre>
=={{header|C++}}==
{{works with|C++11}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <chrono>
#include <atomic>
Line 356 ⟶ 446:
for(auto& t: threads) t.join();
std::cout << "Assembly is finished";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 371 ⟶ 461:
Assembly is finished
</pre>
 
=={{header|Clojure}}==
With a fixed number of workers, this would be very straightforward in Clojure by using a ''CyclicBarrier'' from ''java.util.concurrent''.
So to make it interesting, this version supports workers dynamically joining and parting, and uses the new (2013) ''core.async'' library to use Go-like channels.
Also, each worker passes a value to the checkpoint, so that some ''combine'' function could consume them once they're all received.
<langsyntaxhighlight lang="clojure">(ns checkpoint.core
(:gen-class)
(:require [clojure.core.async :as async :refer [go <! >! <!! >!! alts! close!]]
Line 447 ⟶ 536:
(worker ckpt 10 (monitor 2))))
 
</syntaxhighlight>
</lang>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio;
import std.parallelism: taskPool, defaultPoolThreads, totalCPUs;
 
Line 473 ⟶ 561:
buildMechanism(42);
buildMechanism(11);
}</langsyntaxhighlight>
{{out|Example output}}
<pre>Build detail 0
Line 488 ⟶ 576:
Checkpoint reached. Assemble details ...
Mechanism with 11 parts finished: 55</pre>
 
=={{header|E}}==
 
Line 495 ⟶ 582:
That said, here is an implementation of the task as stated. We start by defining a 'flag set' data structure (which is hopefully also useful for other problems), which allows us to express the checkpoint algorithm straightforwardly while being protected against the possibility of a task calling <code>deliver</code> or <code>leave</code> too many times. Note also that each task gets its own reference denoting its membership in the checkpoint group; thus it can only speak for itself and not break any global invariants.
 
<langsyntaxhighlight lang="e">/** A flagSet solves this problem: There are N things, each in a true or false
* state, and we want to know whether they are all true (or all false), and be
* able to bulk-change all of them, and all this without allowing double-
Line 608 ⟶ 695:
waits with= makeWorker(piece, checkpoint)
}
interp.waitAtTop(promiseAllFulfilled(waits))</langsyntaxhighlight>
 
=={{header|Erlang}}==
A team of 5 workers assemble 3 items. The time it takes to assemble 1 item is 0 - 100 milliseconds.
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( checkpoint_synchronization ).
 
Line 648 ⟶ 734:
end,
worker_loop( Worker, N - 1, Checkpoint ).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 668 ⟶ 754:
Worker 5 item 1
</pre>
=={{header|FreeBASIC}}==
The library ontimer.bi, I have taken it from [https://www.freebasic.net/forum/viewtopic.php?f=7&t=23454 forums of FB].
<syntaxhighlight lang="freebasic">#include "ontimer.bi"
 
Randomize Timer
Dim Shared As Uinteger nWorkers = 3
Dim Shared As Uinteger tID(nWorkers)
Dim Shared As Integer cnt(nWorkers)
Dim Shared As Integer checked = 0
 
Sub checkpoint()
Dim As Boolean sync
If checked = 0 Then sync = False
checked += 1
If (sync = False) And (checked = nWorkers) Then
sync = True
Color 14 : Print "--Sync Point--"
checked = 0
End If
End Sub
 
Sub task(worker As Uinteger)
Redim Preserve cnt(nWorkers)
Select Case cnt(worker)
Case 0
cnt(worker) = Rnd * 3
Color 15 : Print "Worker " & worker & " starting (" & cnt(worker) & " ticks)"
Case -1
Exit Select
Case Else
cnt(worker) -= 1
If cnt(worker) = 0 Then
Color 7 : Print "Worker "; worker; " ready and waiting"
cnt(worker) = -1
checkpoint
cnt(worker) = 0
End If
End Select
End Sub
 
Sub worker1
task(1)
End Sub
Sub worker2
task(2)
End Sub
Sub worker3
task(3)
End Sub
 
Do
OnTimer(500, @worker1, 1)
OnTimer(100, @worker2, 1)
OnTimer(900, @worker3, 1)
Sleep 1000
Loop</syntaxhighlight>
{{out}}
<pre>Worker 1 starting (2 ticks)
Worker 1 ready and waiting
Worker 3 starting (1 ticks)
Worker 3 ready and waiting
--Sync Point--
Worker 3 starting (1 ticks)
Worker 3 ready and waiting
Worker 2 ready and waiting
Worker 1 starting (1 ticks)
Worker 2 starting (0 ticks)
Worker 1 ready and waiting
--Sync Point--
Worker 3 starting (0 ticks)
Worker 2 starting (1 ticks)
Worker 1 starting (1 ticks)
Worker 3 starting (2 ticks)
Worker 2 ready and waiting
Worker 1 ready and waiting
Worker 2 starting (1 ticks)
Worker 1 starting (0 ticks)
Worker 3 ready and waiting
--Sync Point--
Worker 2 ready and waiting
Worker 1 starting (1 ticks)
Worker 3 starting (1 ticks)
Worker 2 starting (3 ticks)
Worker 1 ready and waiting
Worker 3 ready and waiting</pre>
=={{header|Go}}==
'''Solution 1, WaitGroup'''
Line 676 ⟶ 848:
This first solution is a simple interpretation of the task, starting a goroutine (worker) for each part, letting the workers run concurrently, and waiting for them to all indicate completion. This is efficient and idiomatic in Go.
 
<langsyntaxhighlight lang="go">package main
import (
Line 709 ⟶ 881:
log.Println("assemble. cycle", c, "complete")
}
}</langsyntaxhighlight>
{{out}}
Sample run, with race detector option to show no race conditions detected.
Line 751 ⟶ 923:
Channels also synchronize, and in addition can send data. The solution shown here is very similar to the WaitGroup solution above but sends data on a channel to simulate a completed part. The channel operations provide synchronization and a WaitGroup is not needed.
 
<langsyntaxhighlight lang="go">package main
 
import (
Line 790 ⟶ 962:
log.Println(a, "assembled. cycle", c, "complete")
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 832 ⟶ 1,004:
not justified.
 
<langsyntaxhighlight lang="go">package main
 
import (
Line 895 ⟶ 1,067:
close(done)
wg.Wait()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 942 ⟶ 1,114:
 
This solution shows workers joining and leaving, although it is a rather different interpretation of the task.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,009 ⟶ 1,181:
}
l.Println("worker", id, "leaves shop")
}</langsyntaxhighlight>
Output:
<pre>worker 1 contracted to assemble 2 details
Line 1,047 ⟶ 1,219:
worker 6 leaves shop
mechanism 5 completed</pre>
 
=={{header|Haskell}}==
<p>Although not being sure if the approach might be right, this example shows several workers performing a series of tasks simultaneously and synchronizing themselves before starting the next task.</p>
Line 1,063 ⟶ 1,234:
<li>For effectful computations, you should use concurrent threads (forkIO and MVar from the module Control.Concurrent), software transactional memory (STM) or alternatives provided by other modules.</li>
</ul>
<langsyntaxhighlight Haskelllang="haskell">import Control.Parallel
 
data Task a = Idle | Make a
Line 1,145 ⟶ 1,316:
 
main = workshop sum tasks
</syntaxhighlight>
</lang>
<p>The following version works with the concurrency model provided by the module Control.Concurrent</p>
<p>A workshop is an MVar that holds three values: the number of workers doing something, the number of workers ready for the next task and the total number of workers at the moment.</p>
Line 1,155 ⟶ 1,326:
<p>Other than the parallel version above, this code runs in the IO Monad and makes it possible to perform IO actions such as accessing the hardware. However, all actions must have the return type IO (). If the workers must return some useful values, the MVar should be extended with the necessary fields and the workers should use those fields to store the results they produce.</p>
<p>Note: This code has been tested on GHC 7.6.1 and will most probably not run under other Haskell implementations due to the use of some functions from the module Control.Concurrent. It won't work if compiled with the -O2 compiler switch. Compile with the -threaded compiler switch if you want to run the threads in parallel.</p>
<langsyntaxhighlight Haskelllang="haskell">import Control.Concurrent
import Control.Monad -- needed for "forM", "forM_"
 
Line 1,266 ⟶ 1,437:
-- kill all worker threads before exit, if they're still running
forM_ (pids1 ++ pids2) killThread</langsyntaxhighlight>
'''Output:'''
<pre style="height: 200px;overflow:scroll">
Line 1,361 ⟶ 1,532:
The following only works in Unicon:
 
<langsyntaxhighlight lang="unicon">global nWorkers, workers, cv
 
procedure main(A)
Line 1,388 ⟶ 1,559:
wait(cv)
}
end</langsyntaxhighlight>
 
Sample run:
Line 1,416 ⟶ 1,587:
->
</pre>
 
=={{header|J}}==
 
Now that J has a threading implementation: threads may be assigned tasks, and referencing the values produced by the tasks automatically synchronizes.
The current implementations of J are all single threaded. However, the language definition offers a lot of parallelism which I imagine will eventually be supported, after performance gains significantly better than a factor of 2 on common problems become economically viable.
 
For example:
 
<syntaxhighlight lang="j"> {{for. y do. 0 T.'' end.}} 0>.4-1 T.'' NB. make sure we have some threads
For example in 1 2 3 + 4 5 6, we have three addition operations which are specified to be carried out in parallel, and this kind of parallelism pervades the language definition.
ts=: 6!:0 NB. timestamp
dl=: 6!:3 NB. delay
{{r=.EMPTY for. i.y do. dl 1[ r=.r,3}.ts'' end. r}} t. ''"0(3 5)
┌────────────┬────────────┐
│12 53 53.569│12 53 53.569│
│12 53 54.578│12 53 54.578│
│12 53 55.587│12 53 55.587│
│ │12 53 56.603│
│ │12 53 57.614│
└────────────┴────────────┘</syntaxhighlight>
 
Here, we had set up a loop which periodically tracked the time, and waited a second each time through the loop, and repeated the loop a number of times specified at task startup. We ran two tasks, to demonstrate that they were running side-by-side.
=={{header|Java}}==
<langsyntaxhighlight Javalang="java">import java.util.Scanner;
import java.util.Random;
 
Line 1,512 ⟶ 1,696:
public static int nWorkers = 0;
}
}</langsyntaxhighlight>
Output:
<pre style="height: 200px;overflow:scroll">
Line 1,552 ⟶ 1,736:
</pre>
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">import java.util.Random;
import java.util.concurrent.CountDownLatch;
 
Line 1,599 ⟶ 1,783:
}
}
}</langsyntaxhighlight>
Output:
<pre style="height: 200px;overflow:scroll">Starting task 1
Line 1,643 ⟶ 1,827:
Worker 2 is ready
Task 3 complete</pre>
 
=={{header|Julia}}==
Julia has specific macros for checkpoint type synchronization. @async starts an asynchronous task, and multiple @async tasks can be synchronized by wrapping them within the @sync macro statement, which creates a checkpoint for all @async tasks.
<langsyntaxhighlight lang="julia">
function runsim(numworkers, runs)
for count in 1:runs
Line 1,666 ⟶ 1,849:
for trial in trials
runsim(trial[1], trial[2])
end</langsyntaxhighlight>
{{output}}<pre>
Worker 1 finished after 0.2496063425219046 seconds
Line 1,752 ⟶ 1,935:
Finished all runs.
</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// Version 1.2.41
 
import java.util.Random
Line 1,814 ⟶ 1,996:
nTasks = readLine()!!.toInt()
runTasks()
}</langsyntaxhighlight>
 
{{output}}
Line 1,858 ⟶ 2,040:
Worker 5 is ready
</pre>
 
=={{header|Logtalk}}==
The following example can be found in the Logtalk distribution and is used here with permission. It's based on the Erlang solution for this task. Works when using SWI-Prolog, XSB, or YAP as the backend compiler.
<langsyntaxhighlight lang="logtalk">
:- object(checkpoint).
 
Line 1,927 ⟶ 2,108:
 
:- end_object.
</syntaxhighlight>
</lang>
Output:
<langsyntaxhighlight lang="text">
| ?- checkpoint::run.
Worker 1 item 3
Line 1,951 ⟶ 2,132:
All assemblies done.
yes
</syntaxhighlight>
</lang>
=={{header|Nim}}==
As in Oforth, the checkpoint is a thread (the main thread) and synchronization is done using channels:
:– a channel per worker to send orders; an order may be a task number (greater or equal to one) or the stop order (equal to 0);
:– a channel to receive the responses from workers; workers send their identifier (number) via this channel when they have completed a task.
Working on a task is simulated by sleeping during some time (randomly chosen).
 
<syntaxhighlight lang="nim">import locks
import os
import random
import strformat
 
const
NWorkers = 3 # Number of workers.
NTasks = 4 # Number of tasks.
StopOrder = 0 # Order 0 is the request to stop.
 
var
randLock: Lock # Lock to access random number generator.
orders: array[1..NWorkers, Channel[int]] # Channel to send orders to workers.
responses: Channel[int] # Channel to receive responses from workers.
working: int # Current number of workers actually working.
threads: array[1..NWorkers, Thread[int]] # Array of running threads.
 
#---------------------------------------------------------------------------------------------------
 
proc worker(num: int) {.thread.} =
## Worker thread.
 
while true:
# Wait for order from main thread (this is the checkpoint).
let order = orders[num].recv
if order == StopOrder: break
# Get a random time to complete the task.
var time: int
withLock(randLock): time = rand(200..1000)
echo fmt"Worker {num}: starting task number {order}"
# Work on task during "time" ms.
sleep(time)
echo fmt"Worker {num}: task number {order} terminated after {time} ms"
# Send message to indicate that the task is terminated.
responses.send(num)
 
#---------------------------------------------------------------------------------------------------
 
# Initializations.
randomize()
randLock.initLock()
for num in 1..NWorkers:
orders[num].open()
responses.open()
 
# Create the worker threads.
for num in 1..NWorkers:
createThread(threads[num], worker, num)
 
# Send orders and wait for responses.
for task in 1..NTasks:
echo fmt"Sending order to start task number {task}"
# Send order (task number) to workers.
for num in 1..NWorkers:
orders[num].send(task)
working = NWorkers # All workers are now working.
# Wait to receive responses from workers.
while working > 0:
discard responses.recv() # Here, we don't care about the message content.
dec working
 
# We have terminated: send stop order to workers.
echo "Sending stop order to workers."
for num in 1..NWorkers:
orders[num].send(StopOrder)
joinThreads(threads)
echo "All workers stopped."
 
# Clean up.
for num in 1..NWorkers:
orders[num].close()
responses.close()
deinitLock(randLock)</syntaxhighlight>
 
{{out}}
<pre>Sending order to start task number 1
Worker 1: starting task number 1
Worker 2: starting task number 1
Worker 3: starting task number 1
Worker 2: task number 1 terminated after 656 ms
Worker 1: task number 1 terminated after 665 ms
Worker 3: task number 1 terminated after 984 ms
Sending order to start task number 2
Worker 2: starting task number 2
Worker 1: starting task number 2
Worker 3: starting task number 2
Worker 1: task number 2 terminated after 480 ms
Worker 3: task number 2 terminated after 583 ms
Worker 2: task number 2 terminated after 778 ms
Sending order to start task number 3
Worker 1: starting task number 3
Worker 2: starting task number 3
Worker 3: starting task number 3
Worker 3: task number 3 terminated after 472 ms
Worker 1: task number 3 terminated after 545 ms
Worker 2: task number 3 terminated after 894 ms
Sending order to start task number 4
Worker 3: starting task number 4
Worker 2: starting task number 4
Worker 1: starting task number 4
Worker 3: task number 4 terminated after 412 ms
Worker 1: task number 4 terminated after 436 ms
Worker 2: task number 4 terminated after 682 ms
Sending stop order to workers.
All workers stopped.</pre>
=={{header|Oforth}}==
Checkpoint is implemented as a task. It :
Line 1,968 ⟶ 2,259:
- And waits for $allDone checkpoint return on its personal channel.
 
<langsyntaxhighlight Oforthlang="oforth">: task(n, jobs, myChannel)
while(true) [
System.Out "TASK " << n << " : Beginning my work..." << cr
Line 1,990 ⟶ 2,281:
 
#[ checkPoint(n, jobs, channels) ] &
n loop: i [ #[ task(i, jobs, channels at(i)) ] & ] ;</langsyntaxhighlight>
 
=={{header|Perl}}==
 
The perlipc man page details several approaches to interprocess communication. Here's one of my favourites: socketpair and fork. I've omitted some error-checking for brevity.
 
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use strict;
Line 2,064 ⟶ 2,354:
# workers had terminate, it would need to reap them to avoid zombies:
 
wait; wait;</langsyntaxhighlight>
 
A sample run:
Line 2,074 ⟶ 2,364:
msl@64Lucid:~/perl$
</pre>
 
=={{header|Phix}}==
Simple multitasking solution: no locking required, no race condition possible, supports workers leaving and joining.
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>-- demo\rosetta\checkpoint_synchronisation.exw
<span style="color: #000080;font-style:italic;">-- demo\rosetta\checkpoint_synchronisation.exw</span>
constant NPARTS = 3
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- task_xxx(), get_key()</span>
integer workers = 0
<span style="color: #008080;">constant</span> <span style="color: #000000;">NPARTS</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span>
sequence waiters = {}
<span style="color: #004080;">integer</span> <span style="color: #000000;">workers</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
bool terminate = false
<span style="color: #004080;">sequence</span> <span style="color: #000000;">waiters</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
 
<span style="color: #004080;">bool</span> <span style="color: #000000;">terminate</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
procedure checkpoint(integer task_id)
if length(waiters)+1=NPARTS or terminate then
<span style="color: #008080;">procedure</span> <span style="color: #000000;">checkpoint</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">task_id</span><span style="color: #0000FF;">)</span>
printf(1,"checkpoint\n")
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">waiters</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">=</span><span style="color: #000000;">NPARTS</span> <span style="color: #008080;">or</span> <span style="color: #000000;">terminate</span> <span style="color: #008080;">then</span>
for i=1 to length(waiters) do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"checkpoint\n"</span><span style="color: #0000FF;">)</span>
task_schedule(waiters[i],1)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">waiters</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">task_schedule</span><span style="color: #0000FF;">(</span><span style="color: #000000;">waiters</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
waiters = {}
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
else
<span style="color: #000000;">waiters</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
waiters &= task_id
<span style="color: #008080;">else</span>
task_suspend(task_id)
<span style="color: #000000;">waiters</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">task_id</span>
task_yield()
<span style="color: #000000;">task_suspend</span><span style="color: #0000FF;">(</span><span style="color: #000000;">task_id</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">task_yield</span><span style="color: #0000FF;">()</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
procedure worker(string name)
printf(1,"worker %s running\n",{name})
<span style="color: #008080;">procedure</span> <span style="color: #000000;">worker</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">)</span>
while not terminate do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"worker %s running\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">})</span>
printf(1,"worker %s begins part\n",{name})
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">terminate</span> <span style="color: #008080;">do</span>
task_delay(rnd())
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"worker %s begins part\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">})</span>
printf(1,"worker %s completes part\n",{name})
<span style="color: #000000;">task_delay</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">rnd</span><span style="color: #0000FF;">())</span>
checkpoint(task_self())
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"worker %s completes part\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">})</span>
if rnd()>0.95 then exit end if
<span style="color: #000000;">checkpoint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">task_self</span><span style="color: #0000FF;">())</span>
task_delay(rnd())
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">task_self</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">waiters</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #008080;">if</span> <span style="color: #000000;">terminate</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">rnd</span><span style="color: #0000FF;">()></span><span style="color: #000000;">0.95</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"worker %s leaves\n",{name})
<span style="color: #000000;">task_delay</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">rnd</span><span style="color: #0000FF;">())</span>
workers -= 1
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end procedure
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"worker %s leaves\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">})</span>
 
<span style="color: #000000;">workers</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
string name = "A"
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
 
while get_key()!=#1B do -- (key escape to shut down)
<span style="color: #004080;">string</span> <span style="color: #000000;">name</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"A"</span>
if workers<NPARTS then
integer task_id = task_create(routine_id("worker"),{name})
<span style="color: #008080;">while</span> <span style="color: #7060A8;">get_key</span><span style="color: #0000FF;">()!=</span><span style="color: #000000;">#1B</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (key escape to shut down)</span>
task_schedule(task_id,1)
<span style="color: #008080;">if</span> <span style="color: #000000;">workers</span><span style="color: #0000FF;"><</span><span style="color: #000000;">NPARTS</span> <span style="color: #008080;">then</span>
name[1] += 1
<span style="color: #004080;">integer</span> <span style="color: #000000;">task_id</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">task_create</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">routine_id</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"worker"</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">})</span>
workers += 1
<span style="color: #000000;">task_schedule</span><span style="color: #0000FF;">(</span><span style="color: #000000;">task_id</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">name</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
task_yield()
<span style="color: #000000;">workers</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"escape keyed\n")
<span style="color: #000000;">task_yield</span><span style="color: #0000FF;">()</span>
terminate = true
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
while workers>0 do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"escape keyed\n"</span><span style="color: #0000FF;">)</span>
task_yield()
<span style="color: #000000;">terminate</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
end while</lang>
<span style="color: #008080;">while</span> <span style="color: #000000;">workers</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">task_yield</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
<pre style="height: 200px;overflow:scroll">
Line 2,202 ⟶ 2,497:
worker B leaves
</pre>
 
=={{header|PicoLisp}}==
The following solution implements each worker as a coroutine. Therefore, it
Line 2,214 ⟶ 2,508:
'worker' takes a number of steps to perform. It "works" by printing each step,
and returning NIL when done.
<langsyntaxhighlight PicoLisplang="picolisp">(de checkpoints (Projects Workers)
(for P Projects
(prinl "Starting project number " P ":")
Line 2,232 ⟶ 2,526:
(yield ID)
(prinl "Worker " ID " step " N) )
NIL ) )</langsyntaxhighlight>
Output:
<pre>: (checkpoints 2 3) # Start two projects with 3 workers
Line 2,264 ⟶ 2,558:
Worker 1 step 4
Project number 2 is done.</pre>
 
=={{header|PureBasic}}==
 
PureBasic normally uses Semaphores and Mutex’s to synchronize parallel systems. This system only relies on semaphores between each thread and the controller (CheckPoint-procedure). For exchanging data a Mutex based message stack could easily be added, both synchronized according to this specific task or non-blocking if each worker could be allowed that freedom.
<langsyntaxhighlight PureBasiclang="purebasic">#MaxWorktime=8000 ; "Workday" in msec
 
; Structure that each thread uses
Line 2,354 ⟶ 2,647:
CheckPoint()
Print("Press ENTER to exit"): Input()
EndIf</langsyntaxhighlight>
<pre style="height: 200px;overflow:scroll">Enter number of workers to use [2-2000]: 5
Work started, 5 workers has been called.
Line 2,477 ⟶ 2,770:
Thread #1 is done.
Press ENTER to exit</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
"""
 
Line 2,513 ⟶ 2,805:
w2.start()
w3.start()
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,529 ⟶ 2,821:
Exiting worker2
</pre>
 
=={{header|Racket}}==
This solution uses a double barrier to synchronize the five threads.
The method can be found on page 41 of the delightful book
[http://greenteapress.com/semaphores/downey08semaphores.pdf "The Little Book of Semaphores"] by Allen B. Downey.
<langsyntaxhighlight lang="racket">
#lang racket
(define t 5) ; total number of threads
Line 2,579 ⟶ 2,870:
(displayln (for/list ([_ t]) (channel-get ch)))
(loop))
</syntaxhighlight>
</lang>
Output:
<langsyntaxhighlight lang="racket">
(1 4 2 0 3)
(6 9 7 8 5)
Line 2,603 ⟶ 2,894:
(97 98 99 95 96)
...
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>my $TotalWorkers = 3;
<lang perl6>use v6;
 
my $TotalWorkers = 3;
my $BatchToRun = 3;
my @TimeTaken = (5..15); # in seconds
Line 2,645 ⟶ 2,933:
}
);
}</langsyntaxhighlight>
{{out}}
<pre>Worker 1 at batch 0 will work for 6 seconds ..
Line 2,669 ⟶ 2,957:
>>>>> batch 2 completed.
</pre>
 
=={{header|Ruby}}==
{{needs-review|Ruby|This code might or might not do the correct task. See comment at [[Talk:{{PAGENAME}}]].}}
 
<langsyntaxhighlight lang="ruby">require 'socket'
 
# A Workshop runs all of its workers, then collects their results. Use
Line 2,830 ⟶ 3,117:
# Remove all workers.
wids.each { |wid| shop.remove wid }
pp shop.work(6)</langsyntaxhighlight>
 
Example of output: <pre>{23187=>[0, 1346269],
Line 2,846 ⟶ 3,133:
4494=>[5, 4, 1166220]}
{}</pre>
=={{header|Rust}}==
<syntaxhighlight lang="rust">
//! We implement this task using Rust's Barriers. Barriers are simply thread synchronization
//! points--if a task waits at a barrier, it will not continue until the number of tasks for which
//! the variable was initialized are also waiting at the barrier, at which point all of them will
//! stop waiting. This can be used to allow threads to do asynchronous work and guarantee
//! properties at checkpoints.
 
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::channel;
use std::sync::{Arc, Barrier};
use std::thread::spawn;
 
use array_init::array_init;
 
pub fn checkpoint() {
const NUM_TASKS: usize = 10;
const NUM_ITERATIONS: u8 = 10;
 
let barrier = Barrier::new(NUM_TASKS);
let events: [AtomicBool; NUM_TASKS] = array_init(|_| AtomicBool::new(false));
 
// Arc for sharing between tasks
let arc = Arc::new((barrier, events));
// Channel for communicating when tasks are done
let (tx, rx) = channel();
for i in 0..NUM_TASKS {
let arc = Arc::clone(&arc);
let tx = tx.clone();
// Spawn a new worker
spawn(move || {
let (ref barrier, ref events) = *arc;
// Assign an event to this task
let event = &events[i];
// Start processing events
for _ in 0..NUM_ITERATIONS {
// Between checkpoints 4 and 1, turn this task's event on.
event.store(true, Ordering::Release);
// Checkpoint 1
barrier.wait();
// Between checkpoints 1 and 2, all events are on.
assert!(events.iter().all(|e| e.load(Ordering::Acquire)));
// Checkpoint 2
barrier.wait();
// Between checkpoints 2 and 3, turn this task's event off.
event.store(false, Ordering::Release);
// Checkpoint 3
barrier.wait();
// Between checkpoints 3 and 4, all events are off.
assert!(events.iter().all(|e| !e.load(Ordering::Acquire)));
// Checkpoint 4
barrier.wait();
}
// Finish processing events.
tx.send(()).unwrap();
});
}
drop(tx);
// The main thread will not exit until all tasks have exited.
for _ in 0..NUM_TASKS {
rx.recv().unwrap();
}
}
 
fn main() {
checkpoint();
}
</syntaxhighlight>
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">import java.util.{Random, Scanner}
 
object CheckpointSync extends App {
Line 2,931 ⟶ 3,285:
runTasks(in.nextInt)
 
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
This implementation works by having a separate thread handle the synchronization (inter-thread message delivery already being serialized). The alternative, using a read-write mutex, is more complex and more likely to run into trouble with multi-core machines.
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require Thread
 
Line 3,031 ⟶ 3,384:
expr {[llength $members] > 0}
}
}</langsyntaxhighlight>
Demonstration of how this works.
{{trans|Ada}}
<langsyntaxhighlight lang="tcl"># Build the workers
foreach worker {A B C D} {
dict set ids $worker [checkpoint makeThread {
Line 3,063 ⟶ 3,416:
break
}
}</langsyntaxhighlight>
Output:
<pre>
Line 3,106 ⟶ 3,459:
B is ready
D is ready</pre>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="wren">import "random" for Random
import "scheduler" for Scheduler
import "timer" for Timer
import "./ioutil" for Input
 
var rgen = Random.new()
var nWorkers = 0
var nTasks = 0
var nFinished = 0
 
var worker = Fn.new { |id|
var workTime = rgen.int(100, 1000) // 100..999 msec.
System.print("Worker %(id) will work for %(workTime) msec.")
Timer.sleep(workTime)
nFinished = nFinished + 1
System.print("Worker %(id) is ready.")
}
 
var checkPoint = Fn.new {
while (nFinished != nWorkers) {
Timer.sleep(10)
}
nFinished = 0 // reset
}
 
var runTasks = Fn.new {
for (i in 1..nTasks) {
System.print("\nStarting task number %(i).")
var first = rgen.int(1, nWorkers + 1) // randomize first worker to start
// schedule other workers to start while another fiber is sleeping
for (j in 1..nWorkers) {
if (j != first) Scheduler.add { worker.call(j) }
}
worker.call(first) // start first worker
checkPoint.call() // start checkPoint
}
}
 
nWorkers = Input.integer("Enter number of workers to use: ", 1)
nTasks = Input.integer("Enter number of tasks to complete: ", 1)
runTasks.call()</syntaxhighlight>
 
{{out}}
Sample run:
<pre>
Enter number of workers to use: 5
Enter number of tasks to complete: 3
 
Starting task number 1.
Worker 3 will work for 822 msec.
Worker 1 will work for 127 msec.
Worker 2 will work for 618 msec.
Worker 4 will work for 175 msec.
Worker 5 will work for 402 msec.
Worker 1 is ready.
Worker 4 is ready.
Worker 5 is ready.
Worker 2 is ready.
Worker 3 is ready.
 
Starting task number 2.
Worker 2 will work for 537 msec.
Worker 1 will work for 408 msec.
Worker 3 will work for 878 msec.
Worker 4 will work for 101 msec.
Worker 5 will work for 822 msec.
Worker 4 is ready.
Worker 1 is ready.
Worker 2 is ready.
Worker 5 is ready.
Worker 3 is ready.
 
Starting task number 3.
Worker 4 will work for 568 msec.
Worker 1 will work for 603 msec.
Worker 2 will work for 341 msec.
Worker 3 will work for 250 msec.
Worker 5 will work for 837 msec.
Worker 3 is ready.
Worker 2 is ready.
Worker 4 is ready.
Worker 1 is ready.
Worker 5 is ready.
</pre>
 
=={{header|zkl}}==
Line 3,111 ⟶ 3,551:
The consumer requests a part it doesn't have, waits for a part and puts the received part (which might not be the requested one (if buggy code)) in a bin and assembles the parts into a product.
Repeat until all requested products are made.
<langsyntaxhighlight lang="zkl">const NUM_PARTS=5; // number of parts used to make the product
var requested=Atomic.Int(-1); // the id of the part the consumer needs
var pipe=Thread.Pipe(); // "conveyor belt" of parts to consumer
Line 3,136 ⟶ 3,576:
foreach n in (NUM_PARTS){ product[n]-=1 } // remove parts from bin
}
println("Done"); // but workers are still waiting</langsyntaxhighlight>
An AtomicInt is an integer that does its operations in an atomic fashion. It is used to serialize the producers and consumer.
 
Line 3,154 ⟶ 3,594:
Done
</pre>
{{omit from|Axe}}
 
{{omit from|Maxima}}
{{omit from|ML/I}}
{{omit from|Maxima}}
{{omit from|Axe}}
9,479

edits