Checkpoint synchronization: Difference between revisions
Content added Content deleted
m (→{{header|J}}) |
|||
Line 1,471: | Line 1,471: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
Checkpoint is implemented as a task. It : |
Checkpoint is implemented as a task. It : |
||
- Receives n "jobDone" events from n tasks into a "jobs" channel. |
- Receives n "jobDone" events from n tasks into a "jobs" channel. |
||
- Then sends $allDone event to all tasks so they can work again. |
- Then sends $allDone event to all tasks so they can work again. |
||
Each task : |
Each task : |
||
- Sleeps randomly between 1 and 1000 milliseconds, simulating its job. |
- Sleeps randomly between 1 and 1000 milliseconds, simulating its job. |
||
- Then sends "jobDone" to the checkpoint using "jobs" channel. |
- Then sends "jobDone" to the checkpoint using "jobs" channel. |
||
- And waits for $allDone checkpoint return on its personal channel. |
- And waits for $allDone checkpoint return on its personal channel. |
||
<lang Oforth> |
<lang Oforth>: task(n, jobs, myChannel) |
||
{ |
|||
while(true) [ |
while(true) [ |
||
System.Out "TASK " << n << " : Beginning my work..." << cr |
System.Out "TASK " << n << " : Beginning my work..." << cr |
||
Line 1,494: | Line 1,487: | ||
jobs send($jobDone) drop |
jobs send($jobDone) drop |
||
myChannel receive drop |
myChannel receive drop |
||
] |
] ; |
||
} |
|||
: checkPoint(n, jobs, channels) |
|||
{ |
|||
while(true) [ |
while(true) [ |
||
#[ jobs receive drop ] times(n) |
#[ jobs receive drop ] times(n) |
||
"CHECKPOINT : All jobs done, sending done to all tasks" println |
"CHECKPOINT : All jobs done, sending done to all tasks" println |
||
channels apply(#[ send($allDone) drop ]) |
channels apply(#[ send($allDone) drop ]) |
||
] |
] ; |
||
} |
|||
: testCheckPoint(n) |
|||
{ |
|||
| jobs channels i | |
| jobs channels i | |
||
n seq map(#[ drop Channel new]) ->channels |
n seq map(#[ drop Channel new]) ->channels |
||
Line 1,513: | Line 1,502: | ||
#[ checkPoint(n, jobs, channels) ] & |
#[ checkPoint(n, jobs, channels) ] & |
||
n loop: i [ #[ task(i, jobs, channels at(i)) ] & ] |
n loop: i [ #[ task(i, jobs, channels at(i)) ] & ] ;</lang> |
||
}</lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |