[Haskell-cafe] Questions about threads

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Sun Jul 29 20:21:47 EDT 2007

On Sun, 2007-07-29 at 17:35 -0500, John Goerzen wrote:
> Hi everyone, 
> I have been confused by some things about threads for a long time.  I'm
> hoping someone out there can help clear this up.  I'll clean up and
> document on the wiki if we get conclusive answers.
> So it seems there are four scenarios for firing off threads:
> A) Threaded RTS, forkIO
> B) Threaded RTS, forkOS
> C) Non-threaded RTS, forkIO
> D) Non-threaded RTS, forkOS

If I recall correctly, D throws a runtime error because the guarantees
that forkOS is supposed to provide are impossible without the threaded
rts. (I think)

You generally do not want forkOS. It's really only for wierd foreign
libs that require that they be called from the same OS thread every time
eg because they keep thread local state (like OpenGL).

Using forkOS will get you no extra parallelism. You get parallelism
linking with the threaded rts and running your program using multiple


> So the questions, for each of the four models, are:
> 1) What is the impact of firing off a thread to execute a pure (non-IO)
> computation under each model?  Will multiple pure computations be
> allowed to run in parallel, or will only one run at a time?  (While the
> computation may be outside the IO monad, of course at the end it will
> have to use IO to communicate the result back.)

You only get parallelism (as opposed to concurrency) of pure code when
using the threaded rts, and then only when running the program using
more than one capability (+RTS -N2 -RTS).

> 2) What is the impact of IO under each model?  Will GHC internally use
> select/poll/whatever?  Or will each thread get a dedicated OS thread
> that uses synchronous I/O?

In both ghc only uses on OS thread for IO. In the threaded rts it's an
*additional* OS thread but it's still only one.

In the single threaded rts, it's the rts that does the select/poll. In
the threaded rts it's a Haskell IO manager thread that uses select/poll
on behalf of other Haskell threads that need to block until the
completion of I/O.

> 3) When signals are received, which thread receives them?

Each signal gets handled by a new unbound Haskell thread.

> 4) When forkProcess is executed, which thread(s) are duplicated to the
> forked process?

Only the calling one. All other Haskell threads disappear.

> 5) What does an FFI import "safe" mean under each model?

all Haskell threads block until the foreign call returns.

other Haskell threads continue in parallel.

> 6) What does an FFI import "unsafe" mean under each model?

all Haskell threads block until the foreign call returns.

other Haskell threads in the same 'capability' block until the foreign
call returns. If the program is using more than one capability then
Haskell threads in the other capabilities should continue to run.

In both cases, unsafe should only be used for short-running,
non-blocking foreign calls that do not make callbacks into Haskell.

> 7) What is the expected future level of support for each model?  This is
> of significant concern to me, as it appears that the threaded RTS is
> only supported on an extremely limited set of architectures (most
> programs won't even link on Debian's autobuilders if I use -threaded).
> Also I have heard comments that the non-threaded RTS may be dropped in
> the future.

The non-threaded rts is not going to get many improvements though it
probably will not be dropped while the threaded rts doesn't work on
those other arches. The threaded rts will probably become the default in
some upcoming release.

> 8) What is the expected level of support for STM in combination with
> each threaded model?

No idea, but bear in mind the threaded rts is where the attention is

> 9) How does par mix with each threaded model?  Is it equivolent to
> forkOS or forkIO?



More information about the Haskell-Cafe mailing list