From HaskellWiki
Revision as of 09:49, 3 July 2006 by Simonpj (talk | contribs)
Jump to navigation Jump to search

Concurrent programming in GHC

This page contains notes and information about how to write concurrent programs in GHC.

Please feel free to add stuff here (Edit page link at the bottom).

Starting points

  • Software Transactional Memory (STM) is a new way to coordinate concurrent threads. There's a separate Wiki page devoted to STM.
STM will be in GHC 6.6, and is described in the paper Composable memory transactions. The paper Lock-free data structures using Software Transactional Memory in Haskell gives further examples of concurrent programming using STM.

Using concurrency in GHC

  • The GHC manual gives a few useful flags that control scheduling (not usually necessary) RTS options.

Multiprocessor GHC

As of version 6.5, GHC supports running programs in parallel on an SMP or multi-core machine. How to do it:

  • You need to link your program using the -threaded switch. (NOTE: previously it was necessary to compile all code, including libraries, with the -smp switch, this is no longer the case. The -smp flag is now a synonym for -threaded).
  • Run the program with +RTS -N2 to use 2 threads, for example. You should use a -N value equal to the number of CPU cores on your machine (not including Hyper-threading cores).
  • Concurrent threads (forkIO and forkOS) will run in parallel, and you can also use the par combinator and Strategies from the Control.Parallel.Strategies module to create parallelism.
  • Use +RTS -sstderr for timing stats.

Links to related work on parallel and distributed Haskell (many based on GHC)