Skip to content

Instantly share code, notes, and snippets.

@iokasimov
Created November 20, 2017 13:07
Show Gist options
  • Save iokasimov/cb7295ea1b7ddeff9159f59eb8e5c51d to your computer and use it in GitHub Desktop.
Save iokasimov/cb7295ea1b7ddeff9159f59eb8e5c51d to your computer and use it in GitHub Desktop.
import Data.Monoid
import Control.Concurrent
import qualified Control.Concurrent.Broadcast as Broadcast
biglog n = do
if n == 100000 then pure n
else print n >> biglog (n + 1)
main = do
global_bc <- Broadcast.new -- main thread
print_bc <- Broadcast.new -- thread with printing
-- awake all global-listeners
Broadcast.broadcast global_bc ()
forkIO $ do
-- listen broadcast from main thread
Broadcast.listen global_bc
-- run a long computation
n <- biglog 0
-- if this computation ended early than hitiing timout,
-- broadcast about computed value
Broadcast.broadcast print_bc n
threadDelay 1000000
Broadcast.tryListen print_bc >>= \v -> case v of
Just n -> print $ "managed: " <> (show n)
Nothing -> print "interrupted!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment