Checkpoint synchronization: Difference between revisions

Corrected information about valid compiler switches
(Added Haskell version with concurrency, including workers joining in and leaving the workshop.)
(Corrected information about valid compiler switches)
Line 641:
<p>The "main" function launches three workers first, and after 5 seconds it launches two workers more. It then waits for a key press and kills all threads, if they're still active.</p>
<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. IfIt runwon't fromwork GHCiif orcompiled withoutwith usingthe -O2 compiler switch. Compile with the -threaded compiler switch, theif threadsyou won'twant to run the threads in parallel.</p>
<lang Haskell>import Control.Concurrent
import Control.Monad -- needed for "forM", "forM_"
Line 756:
'''Output:'''
<pre style="height: 200px;overflow:scroll">
main +RTS -N2
 
Worker ThreadId 30 has joined the group.
Worker ThreadId 31 has joined the group.
Anonymous user