Goroutines in Haskell

Our example demonstrates the use of lightweight threads of execution. Here’s the full source code in Haskell:

import Control.Concurrent
import Control.Monad (replicateM_)
import System.IO

f :: String -> IO ()
f from = replicateM_ 3 $ \i -> do
    putStrLn (from ++ " : " ++ show i)

main :: IO ()
main = do
    -- Calling the function synchronously
    f "direct"

    -- Calling the function in a concurrent thread
    forkIO $ f "goroutine"

    -- Starting a concurrent thread with an anonymous function
    forkIO $ putStrLn "going"

    -- Waiting for the threads to finish (naive approach)
    threadDelay 1000000
    putStrLn "done"

Suppose we have a function call f(s). Here’s how we’d call that in the usual way, running it synchronously.

f "direct"

To invoke this function in a concurrent thread, use forkIO $ f(s). This new thread will execute concurrently with the calling one.

forkIO $ f "goroutine"

You can also start a concurrent thread for an anonymous function call.

forkIO $ putStrLn "going"

Our two function calls are running asynchronously in separate threads now. We wait for them to finish. (For a more robust approach, use a synchronization primitive like MVar or Chan).

threadDelay 1000000
putStrLn "done"

When we run this program, we see the output of the blocking call first, then the output of the two concurrent threads. The threads’ output may be interleaved, because threads are being run concurrently by the Haskell runtime.

$ runhaskell example.hs
direct : 0
direct : 1
direct : 2
goroutine : 0
going
goroutine : 1
goroutine : 2
done

Next, we’ll look at a complement to concurrent threads in Haskell programs: channels.