Concurrency and parallelism

Ten lessons on threads in Haskell: forkIO, the MVar, a channel, and STM with its retry and its orElse. The asynchronous exception and the mask come next, then the threaded runtime with +RTS -N, the parallelism this image cannot run, and a worker pool. The part ends on the deadlock and the race that a careful design avoids.

094 forkIO forkIO, threadDelay, killThread, the main thread that must wait Open 095 MVar newEmptyMVar, takeMVar, putMVar, a handoff, a shared counter Open 096 Channels newChan, writeChan, readChan, a producer and a consumer Open 097 STM atomically, TVar, readTVar, writeTVar, a counter that never loses an update Open 098 STM patterns retry, orElse, a bounded queue, a timeout written with STM Open 099 Asynchronous exceptions throwTo, mask, uninterruptibleMask, bracket in a thread, a cancelled worker Open 100 -threaded and the RTS -threaded, +RTS -N, -N2, the timings on this machine, +RTS -s Open 101 Parallelism and what is missing par and pseq need the parallel package, which is not here; chunks over forkIO, the numbers Open 102 A worker pool a queue of jobs, worker threads, a result channel, a shutdown that waits Open 103 Deadlocks and races a deadlock with two MVars, threadDelay in the wrong place, the STM answer Open

Lessons