Skip to content

Instantly share code, notes, and snippets.

@gromakovsky
Created March 6, 2018 20:40
Show Gist options
  • Save gromakovsky/018b4169ae2ab2adffd744ce6d510f3a to your computer and use it in GitHub Desktop.
Save gromakovsky/018b4169ae2ab2adffd744ce6d510f3a to your computer and use it in GitHub Desktop.
#!/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