About Haskell Thread Model

Wolfgang Thaller wolfgang.thaller at gmx.net
Sat Oct 11 23:58:04 EDT 2003

> I am a new learner of Haskell and I am interested in Haskell's
> concurrent model. Can somebody give me a brief intro about Haskell's
> thread model, like how the use-level threads are mapped to kernel 
> thread
> and what scheduling mechanism that Haskell uses, or point me to some
> links or documents that I can learn from.

In the interpreter Hugs, all Haskell threads are run in one kernel 
thread. They are scheduled cooperatively; thread switches only take 
place when a function from the "Concurrent" module is called.

In the currently released version of GHC, all Haskell threads are run 
in one kernel thread, too; however, thread switches can take place 
whenever memory is allocated --- and in Haskell, that means "almost 
always". The optimizer manages to compile a fibonacci function that 
doesn't allocate any memory, but in the real world, it's as good as 
real preemption.

If you compile the bleeding-edge GHC from the CVS HEAD, you'll get 
something else; while "most" threads (those created using "forkIO") are 
still light-weight threads that are scheduled in just one kernel 
thread, you can also create threads that get their own operating system 
thread. This is solves all the problems that lightweight threads can 
have with foreign (i.e. non-Haskell) libraries.

You should also note that no Haskell implementation currently supports 
SMP; even when multiple kernel threads are used, there is a mutual 
exclusion lock on the Haskell heap, so a multithreaded Haskell program 
will use only one CPU on an SMP system.

I hope my answer was useful...



P.S.: If you want to do me a favour, you could tell your mail program 
not to send multipart or HTML messages to the list; they look terrible 
to people like me who get a daily digest from the mailing list.

More information about the Haskell mailing list