File: libs-9.html

package info (click to toggle)
hugs 1.4.199801-1
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 7,220 kB
  • ctags: 5,609
  • sloc: ansic: 32,083; haskell: 12,143; yacc: 949; perl: 823; sh: 602; makefile: 236
file content (115 lines) | stat: -rw-r--r-- 3,647 bytes parent folder | download
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 () -&gt; IO ThreadId
killThread       :: ThreadId -&gt; IO ()

data MVar a      -- Synchronisation variables
newEmptyMVar     :: IO (MVar a)
newMVar          :: a -&gt; IO (MVar a)
takeMVar         :: MVar a -&gt; IO a
putMVar          :: MVar a -&gt; a -&gt; IO ()
swapMVar         :: MVar a -&gt; a -&gt; IO a
readMVar         :: MVar a -&gt; IO a 
instance Eq (MVar a)

data Chan a      -- channels
newChan          :: IO (Chan a)
writeChan        :: Chan a -&gt; a -&gt; IO ()
readChan         :: Chan a -&gt; IO a
dupChan          :: Chan a -&gt; IO (Chan a)
unReadChan       :: Chan a -&gt; a -&gt; IO ()
getChanContents  :: Chan a -&gt; IO [a]
writeList2Chan   :: Chan a -&gt; [a] -&gt; IO ()
                      
data CVar a       -- one element channels
newCVar          :: IO (CVar a)
putCVar          :: CVar a -&gt; a -&gt; IO ()
getCVar          :: CVar a -&gt; IO a
                      
data QSem        -- General/quantity semaphores
newQSem          :: Int  -&gt; IO QSem
waitQSem         :: QSem -&gt; IO ()
signalQSem       :: QSem -&gt; IO ()
                      
data QSemN       -- General/quantity semaphores
newQSemN         :: Int   -&gt; IO QSemN
waitQSemN        :: QSemN -&gt; Int -&gt; IO ()
signalQSemN      :: QSemN -&gt; Int -&gt; IO ()

type SampleVar a -- Sample variables 
newEmptySampleVar:: IO (SampleVar a)
newSampleVar     :: a -&gt; IO (SampleVar a)
emptySampleVar   :: SampleVar a -&gt; IO ()
readSampleVar    :: SampleVar a -&gt; IO a
writeSampleVar   :: SampleVar a -&gt; a -&gt; 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') &gt;&gt; write 'b'
 where write c = putChar c &gt;&gt; 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>