forkIO

forkIO starts a thread

forkIO takes an IO action, starts a new lightweight thread for it and returns a ThreadId. The action runs when the runtime schedules it, which is after the call returns and never before:

forkIO :: IO () -> IO ThreadId threadDelay :: Int -> IO () killThread :: ThreadId -> IO ()

Haskell threads are not operating system threads: the runtime multiplexes thousands of them over the capabilities it has, and a thread that blocks on an MVar or a delay costs nothing. threadDelay takes microseconds, so 100000 is a tenth of a second.

A worker and a main thread

module Main (main) where import Control.Concurrent (forkIO, newEmptyMVar, putMVar, takeMVar, threadDelay) main :: IO () main = do done <- newEmptyMVar putStrLn "main waits" _ <- forkIO $ do putStrLn "worker starts" threadDelay 100000 putStrLn "worker ends" putMVar done () takeMVar done putStrLn "main ends"

ghc -Wall -O1 -threaded -o fork fork.hs
./fork

main waits
worker starts
worker ends
main ends

The order is the order of the program: main prints first, the worker prints its two lines around the delay, and main finishes last because takeMVar blocked until the worker put a value. The MVar here is a one-shot signal, and the next lesson is about it as a data structure; in this program it is what keeps the main thread alive.

The thread that must wait

A program ends when its main thread ends, and the other threads are killed with it. Without the takeMVar, the program would usually print main waits and exit before the worker did anything: there is no join and no supervision, and that is a feature, not an oversight.

The threaded runtime

-threaded links the runtime that can use several operating-system threads, and +RTS -N sets how many. Without -threaded the program still runs with green threads, one at a time on one core; with it and -N4 four green threads can run at once, which the lesson on the threaded runtime measures on this machine.

The build here asks for -threaded because the program is the smallest one that demonstrates a second thread; a program that only blocks on MVars does not need it, and a program that computes in parallel does.

Exercises

  1. Start two workers that each print a line and remove the takeMVar: describe what you see.
  2. Use threadDelay in the main thread instead of the MVar and explain why it is a worse signal.
  3. Keep the ThreadId and kill the worker before it finishes.
  4. Print the thread id of the main thread with myThreadId.
  5. Explain why a forked thread that throws does not stop the program.
  6. Build without -threaded and run with +RTS -N4; read the error.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does forkIO action return?

    Answer

    The ThreadId of the new thread

    The ThreadId is what killThread and throwTo need later.

  2. When does the forked action start?

    Answer

    After forkIO returns, when the runtime schedules it

    forkIO puts the thread on the run queue; it does not run the action inline.

  3. Why does the program end when main returns?

    Answer

    The main thread is the lifetime of the process and the others are killed

    The runtime ends the program with the main thread, which is why the lesson waits on an MVar.

  4. What does this program print? module Main (main) where import Control.Concurrent (forkIO, newEmptyMVar, putMVar, takeMVar) main :: IO () main = do box <- newEmptyMVar _ <- forkIO (putMVar box "worker") takeMVar box >>= putStrLn

    Answer

    worker

    The worker fills the MVar and the main thread prints what it took.

  5. What does threadDelay 100000 wait?

    Answer

    A tenth of a second

    The argument is microseconds, so 100000 is 0.1 s.

  6. What does this program print? module Main (main) where import Control.Concurrent (forkIO, threadDelay) main :: IO () main = do putStrLn "a" _ <- forkIO (threadDelay 1000 >> putStrLn "b") putStrLn "c" threadDelay 100000

    Answer

    a c b

    The main thread prints a and c, then its delay lets the worker print b: the lines are a, c, b.

  7. What does -threaded add?

    Answer

    A runtime that can run green threads on several OS threads

    Without it the green threads run on one capability; with it -N chooses how many.

  8. What does this program print? module Main (main) where main :: IO () main = print (1 + 1)

    Answer

    2

    The sum is printed without quotes.

  9. What happens to an uncaught exception in a forked thread?

    Answer

    The thread dies and the runtime prints it; the main thread continues

    The exception belongs to the thread, which is why a supervisor is a design decision.

  10. What is a Haskell thread?

    Answer

    A lightweight thread scheduled by the runtime on the capabilities

    The runtime multiplexes many green threads, which is why a blocked one is cheap.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev