Created
November 20, 2017 13:07
-
-
Save iokasimov/cb7295ea1b7ddeff9159f59eb8e5c51d 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
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