About Haskell Thread Model

Yang, Qing qing.yang at intel.com
Mon Oct 13 15:08:31 EDT 2003


Thanks very much. It's very useful.

PS: Sorry for the inconvenience I think I should have sent the mail in
plain text.

Qing 

-----Original Message-----
From: Wolfgang Thaller [mailto:wolfgang.thaller at gmx.net] 
Sent: Sunday, October 12, 2003 4:58 AM
To: Yang, Qing
Cc: haskell at haskell.org
Subject: Re: About Haskell Thread Model

> 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...

Cheers,

Wolfgang

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