How difficult is Haskell multi-threading?

Mantas Vidutis picture Mantas Vidutis · Jun 10, 2010 · Viewed 19.6k times · Source

I have heard that in Haskell, creating a multi-threaded application is as easy as taking a standard Haskell application and compiling it with the -threaded flag. Other cases, however, have described the use of a par command within the actual source code.

What is the state of Haskell multi-threading? How easy is it to introduce into programs? Is there a good multi-threading tutorial that goes over these different commands and their uses?

Answer

Don Stewart picture Don Stewart · Jun 10, 2010

What is the state of Haskell multi-threading?

Mature. The implementation is around 15 years old, with transactional memory for 5 years. GHC is a widely used compiler, with large open source support, and commercial backing.

How easy is it to introduce into programs?

This depends on the algorithm. Sometimes it can be a one line use of par to gain parallelism. Sometimes new algorithms must be developed. In general it will be easier to introduce safe parallelism and concurrency in Haskell, than in typical languages, and performance is good.

Is there a good multi-threading tutorial that goes over these different commands and their uses?

There are 3 major parallel and concurrent programming models in Haskell.

  • implicit parallelism via par
  • explicit concurrency and parallelism via forkIO / MVars and software transactional memory
  • data parallelism via the DPH libraries

These are the main things. In all cases you compile with -threaded to use the multicore runtime, but how easy it is to parallelise a particular problem depends on the algorithm you use, and the parallel programming model you adopt from that list.

Here is an introduction to the main parallel programming models in Haskell, and how to achieve speedups.

I think Chapter 24 of Real World Haskell is a good tutorial.