Skip to content
Snippets Groups Projects
Synchronized.hs 418 B
Newer Older
Martin Ring's avatar
Martin Ring committed
import qualified Sem
import Control.Concurrent 

sync :: Sem.Sem-> IO ()-> IO ()
sync s f = do Sem.wait s; f; Sem.signal s

b1 :: IO ()
b1 = do putStrLn "foo"; threadDelay(1000); putStrLn "baz"

-- Synchronized
s1 :: IO ()
s1 = do q<- Sem.new; forkIO (sync q b1); sync q b1

-- Unsynchronized
us1 :: IO ()
us1 = do q<- Sem.new; forkIO b1; b1

{- This also works:
q<- Sem.newQSem 1
forkIO (sync q b1) >> (sync q b1)
-}