-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSample.hs
More file actions
23 lines (22 loc) · 773 Bytes
/
Sample.hs
File metadata and controls
23 lines (22 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import qualified Data.IVar.Simple as IVar
import Control.Concurrent
main :: IO ()
main = do
-- create new `IVar`
iv <- IVar.new
-- spawn a thread that reads the `IVar` and prints the value
forkIO $ print (IVar.read iv)
-- the spawned thread will sleep while the `IVar` is empty
threadDelay 1000000
-- tentatively read the `IVar` -- it's still empty
IVar.tryRead iv >>= print
-- write a value to the `IVar`
IVar.write iv 42
-- now the thread will be woken up and print "42"
threadDelay 1000000
-- tentatively read the `IVar` -- now it's full
IVar.tryRead iv >>= print
-- further writes fail: `tryWrite` returns `False`
IVar.tryWrite iv 42 >>= print
-- and `write` throws an exception
IVar.write iv 42