Skip to content

Instantly share code, notes, and snippets.

@CodingCellist
Created June 11, 2021 09:56
Show Gist options
  • Save CodingCellist/618ccf6562ac8133bda4a58207e4a128 to your computer and use it in GitHub Desktop.
Save CodingCellist/618ccf6562ac8133bda4a58207e4a128 to your computer and use it in GitHub Desktop.
Idris2 channels under the Chez-Scheme CG might not be working correctly...
import System.Concurrency
import Data.List
data Msg = Continue
| Stop
receiver : Channel Msg -> Bool -> IO ()
receiver c first =
do Continue <- channelGet c
| Stop => putStrLn "Thread done."
receiver c False
nThreads : Nat
nThreads = 4
nMsgs : Nat
nMsgs = 8
stopLoop : Channel Msg -> Nat -> IO ()
stopLoop c 0 = pure ()
stopLoop c (S k) =
do channelPut c Stop
stopLoop c k
main : IO ()
main = do c <- makeChannel
tIds <- for (replicate nThreads Z) $ \_ => fork (receiver c True)
putStrLn $ "-- Successfully created " ++ show (length tIds) ++ " threads"
ignore $ traverse (channelPut c) $ replicate nMsgs Continue
putStrLn $ "-- Sending " ++ show nThreads ++ " Stop msgs."
stopLoop c nThreads
ignore $ traverse (\t => threadWait t) tIds
putStrLn "Test successful!"
import System.Concurrency
import Data.List
data Msg = Continue
| Stop
receiver : Channel Msg -> Bool -> IO ()
receiver c first =
do case first of
True => do putStrLn "New thread!"
receiver c False
False => do Continue <- channelGet c
| Stop => putStrLn "Thread done."
receiver c False
nThreads : Nat
nThreads = 4
nMsgs : Nat
nMsgs = 8
stopLoop : Channel Msg -> Nat -> IO ()
stopLoop c 0 = putStrLn "--- stopLoop done"
stopLoop c (S k) =
do putStrLn "--- Sending Stop"
channelPut c Stop
stopLoop c k
main : IO ()
main = do c <- makeChannel
tIds <- for (replicate nThreads Z) $ \_ => fork (receiver c True)
putStrLn $ "-- Successfully created " ++ show (length tIds) ++ " threads"
ignore $ traverse (channelPut c) $ replicate nMsgs Continue
putStrLn "-- Stopping the threads..."
stopLoop c nThreads
putStrLn $ "-- Sent " ++ show nThreads ++ " Stop msgs."
ignore $ traverse (\t => threadWait t) tIds
putStrLn "Test successful!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment