Chapter 13. Parallel Programming Using Threads
We have been discussing concurrency as a means to modularize programs with multiple interactions. For instance, concurrency allows a network server to interact with a multitude of clients simultaneously while letting you separately write and maintain code that deals with only a single client at a time. Sometimes these interactions are batch-like operations that we want to overlap, such as when downloading multiple URLs simultaneously. There the goal was to speed up the program by overlapping the I/O, but it is not true parallelism because we don’t need multiple processors to achieve a speedup; the speedup was obtained by overlapping the time spent waiting for multiple web servers to respond.
But concurrency can also be used to achieve true parallelism. In
this book, we have tried to emphasize the use of the parallel
programming models—Eval, Strategies, the Par monad, and so on—for
parallelism where possible, but there are some problems for which
these pure parallel programming models cannot be used. These are the two main
classes of problem:
- Problems where the work involves doing some I/O
- Algorithms that rely on some nondeterminism internally
Having side effects does not necessarily rule out the use of
parallel programming models because Haskell has the ST monad for
encapsulating side-effecting computations. However, it is typically
difficult to use parallelism within the ST monad, and in that case probably the only solution is to drop ...