Created
March 6, 2018 20:40
-
-
Save gromakovsky/018b4169ae2ab2adffd744ce6d510f3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
{- stack | |
script | |
--resolver lts-9.1 | |
--package base | |
--package async | |
-} | |
import Control.Exception | |
import Control.Concurrent | |
import Control.Concurrent.Async | |
race0 left right = | |
withAsync left $ \a -> | |
withAsync right $ \b -> | |
waitEither a b | |
main :: IO () | |
main = () <$ bracket acquire release action | |
where | |
sleepForever = threadDelay 100500100500 | |
acquire = putStrLn "acquire main" | |
release _ = putStrLn "release main" | |
action _ = race0 a b | |
a = sleepForever | |
b = bracket acquireB releaseB (const sleepForever) | |
acquireB = putStrLn "acquire B" | |
releaseB _ = putStrLn "release B" >> threadDelay 1000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment