1 <sect> <idx/Concurrent/
2 <label id="sec:Concurrent">
5 This library provides the Concurrent Haskell extensions as described
6 in <url name="Concurrent Haskell" url="http://research.microsoft.com/Users/simonpj/Papers/concurrent-haskell.ps.gz">.
9 module Concurrent where
11 data ThreadId -- thread identifiers
15 forkIO :: IO () -> IO ThreadId
16 killThread :: ThreadId -> IO ()
18 data MVar a -- Synchronisation variables
19 newEmptyMVar :: IO (MVar a)
20 newMVar :: a -> IO (MVar a)
21 takeMVar :: MVar a -> IO a
22 putMVar :: MVar a -> a -> IO ()
23 swapMVar :: MVar a -> a -> IO a
24 readMVar :: MVar a -> IO a
25 isEmptyMVar :: MVar a -> IO Bool
29 data Chan a -- channels
30 newChan :: IO (Chan a)
31 writeChan :: Chan a -> a -> IO ()
32 readChan :: Chan a -> IO a
33 dupChan :: Chan a -> IO (Chan a)
34 unReadChan :: Chan a -> a -> IO ()
35 getChanContents :: Chan a -> IO [a]
36 writeList2Chan :: Chan a -> [a] -> IO ()
38 data CVar a -- one element channels
39 newCVar :: IO (CVar a)
40 putCVar :: CVar a -> a -> IO ()
41 getCVar :: CVar a -> IO a
43 data QSem -- General/quantity semaphores
44 newQSem :: Int -> IO QSem
45 waitQSem :: QSem -> IO ()
46 signalQSem :: QSem -> IO ()
48 data QSemN -- General/quantity semaphores
49 newQSemN :: Int -> IO QSemN
50 waitQSemN :: QSemN -> Int -> IO ()
51 signalQSemN :: QSemN -> Int -> IO ()
53 type SampleVar a -- Sample variables
54 newEmptySampleVar:: IO (SampleVar a)
55 newSampleVar :: a -> IO (SampleVar a)
56 emptySampleVar :: SampleVar a -> IO ()
57 readSampleVar :: SampleVar a -> IO a
58 writeSampleVar :: SampleVar a -> a -> IO ()
65 GHC uses preemptive multitasking:
66 Context switches can occur at any time, except if you call a C
67 function (like <tt/getchar/) that blocks waiting for input.
69 Hugs uses cooperative multitasking:
70 Context switches only occur when you use one of the primitives
71 defined in this module. This means that programs such as:
72 s not been implemented yet on
75 main = forkIO (write 'a') >> write 'b'
76 where write c = putChar c >> write c
79 will print either <tt/aaaaaaaaaaaaaa.../ or <tt/bbbbbbbbbbbb.../,
80 instead of some random interleaving of <tt/a/s and <tt/b/s.
82 In practice, cooperative multitasking is sufficient for writing
83 simple graphical user interfaces.
86 Hugs does not provide the functions <tt/mergeIO/ or <tt/nmergeIO/ since these
87 require preemptive multitasking.
90 Thread identities and <tt/killThread/ have an experimental
91 implementation in GHC, but are not yet implemented in Hugs.
93 Currently <tt/killThread/ simply kills the nominated thread, but the
94 plan is that in the future <tt/killThread/ will raise an exception in
95 the killed thread which it can catch --- perhaps allowing it to kill
96 its children before exiting.
99 The <tt/Ord/ instance for <tt/ThreadId/s provides an arbitrary total ordering
100 which might be used to build an ordered binary tree, say.
103 The operation <tt/isEmptyMVar/ returns a flag indicating
104 whether the <tt/MVar/ is currently empty or filled in, i.e.,
105 will a thread block when performing a <tt/takeMVar/ on that
108 Please notice that the Boolean value returned from <tt/isEmptyMVar/
109 represent just a snapshot of the state of the <tt/MVar/. By the
110 time a thread gets to inspect the result and act upon it, other
111 threads may have accessed the <tt/MVar/ and changed its 'filled-in'
112 status of the variable. Please be wary of this.