Skip to content

Instantly share code, notes, and snippets.

@evanrelf
Created February 15, 2025 02:10
Show Gist options
  • Save evanrelf/f33502a06c91270970f4a0de34bacf33 to your computer and use it in GitHub Desktop.
Save evanrelf/f33502a06c91270970f4a0de34bacf33 to your computer and use it in GitHub Desktop.
#!/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