Created
February 15, 2025 02:10
-
-
Save evanrelf/f33502a06c91270970f4a0de34bacf33 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 runghc | |
-- ki, unfork, stm | |
{-# LANGUAGE BlockArguments #-} | |
{-# OPTIONS_GHC -Wall #-} | |
{-# OPTIONS_GHC -threaded #-} | |
import Control.Concurrent qualified as Concurrent | |
import Control.Concurrent.STM qualified as STM | |
import Data.Foldable (for_) | |
import Ki qualified | |
import Prelude hiding (log) | |
import Unfork (unforkSyncIO_) | |
main :: IO () | |
main = do | |
log <- unforkSyncIO_ putStrLn | |
log "parent: begin" | |
Ki.scoped \scope -> do | |
log "parent: spawning begin" | |
for_ [1 .. 10 :: Int] \i -> | |
Ki.fork scope do | |
log $ "child" <> show i <> ": begin" | |
Concurrent.threadDelay 2_000_000 | |
log $ "child" <> show i <> ": end" | |
log "parent: spawning end" | |
STM.atomically $ Ki.awaitAll scope | |
log "parent: end" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment