1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
<HTML>
<HEAD>
<TITLE>The Hugs-GHC Extension Libraries: Concurrent </TITLE>
</HEAD>
<BODY>
<A HREF="libs-8.html">Previous</A>
<A HREF="libs-10.html">Next</A>
<A HREF="libs.html#toc9">Table of Contents</A>
<HR>
<H2><A NAME="s9">9. Concurrent </A></H2>
<P>This library provides the Concurrent Haskell extensions
</I>].</P>
<P>We are grateful to the Glasgow Haskell Project for allowing us to
redistribute their implementation of this module.</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
module Concurrent where
data ThreadId -- thread identifiers
instance Eq ThreadId
instance Ord ThreadId
forkIO :: IO () -> IO ThreadId
killThread :: ThreadId -> IO ()
data MVar a -- Synchronisation variables
newEmptyMVar :: IO (MVar a)
newMVar :: a -> IO (MVar a)
takeMVar :: MVar a -> IO a
putMVar :: MVar a -> a -> IO ()
swapMVar :: MVar a -> a -> IO a
readMVar :: MVar a -> IO a
instance Eq (MVar a)
data Chan a -- channels
newChan :: IO (Chan a)
writeChan :: Chan a -> a -> IO ()
readChan :: Chan a -> IO a
dupChan :: Chan a -> IO (Chan a)
unReadChan :: Chan a -> a -> IO ()
getChanContents :: Chan a -> IO [a]
writeList2Chan :: Chan a -> [a] -> IO ()
data CVar a -- one element channels
newCVar :: IO (CVar a)
putCVar :: CVar a -> a -> IO ()
getCVar :: CVar a -> IO a
data QSem -- General/quantity semaphores
newQSem :: Int -> IO QSem
waitQSem :: QSem -> IO ()
signalQSem :: QSem -> IO ()
data QSemN -- General/quantity semaphores
newQSemN :: Int -> IO QSemN
waitQSemN :: QSemN -> Int -> IO ()
signalQSemN :: QSemN -> Int -> IO ()
type SampleVar a -- Sample variables
newEmptySampleVar:: IO (SampleVar a)
newSampleVar :: a -> IO (SampleVar a)
emptySampleVar :: SampleVar a -> IO ()
readSampleVar :: SampleVar a -> IO a
writeSampleVar :: SampleVar a -> a -> IO ()
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>Notes:
<UL>
<LI>
GHC uses preemptive multitasking:
Context switches can occur at any time, except if you call a C
function (like \verb"getchar") that blocks waiting for input.
Hugs uses cooperative multitasking:
Context switches only occur when you use one of the primitives
defined in this module. This means that programs such as:
<BLOCKQUOTE><CODE>
<PRE>
main = forkIO (write 'a') >> write 'b'
where write c = putChar c >> write c
</PRE>
</CODE></BLOCKQUOTE>
will print either <CODE>aaaaaaaaaaaaaa...</CODE> or <CODE>bbbbbbbbbbbb...</CODE>,
instead of some random interleaving of <CODE>a</CODE>s and <CODE>b</CODE>s.
In practice, cooperative multitasking is sufficient for writing
simple graphical user interfaces.
</LI>
<LI>Hugs does not provide the functions <CODE>mergeIO</CODE> or <CODE>nmergeIO</CODE> since these
require preemptive multitasking.
</LI>
<LI>Thread identities and <CODE>killThread</CODE> has not been implemented yet on
either system. The plan is that <CODE>killThread</CODE> will raise an IO
exception in the killed thread which it can catch --- perhaps allowing -->
--it to kill its children before exiting.
</LI>
<LI>The <CODE>Ord</CODE> instance for <CODE>ThreadId</CODE>s provides an arbitrary total ordering
which might be used to build an ordered binary tree, say.
</LI>
</UL>
</P>
<HR>
<A HREF="libs-8.html">Previous</A>
<A HREF="libs-10.html">Next</A>
<A HREF="libs.html#toc9">Table of Contents</A>
</BODY>
</HTML>
|