Skip to Main Content
Parallel and Concurrent Programming in Haskell
book

Parallel and Concurrent Programming in Haskell

by Simon Marlow
July 2013
Intermediate to advanced content levelIntermediate to advanced
322 pages
8h 43m
English
O'Reilly Media, Inc.
Content preview from Parallel and Concurrent Programming in Haskell

Chapter 10. Software Transactional Memory

Software transactional memory (STM) is a technique for simplifying concurrent programming by allowing multiple state-changing operations to be grouped together and performed as a single atomic operation. Strictly speaking, “software transactional memory” is an implementation technique, whereas the language construct we are interested in is “atomic blocks.” Unfortunately, the former term has stuck, and so the language-level facility is called STM.

STM solves a number of problems that arise with conventional concurrency abstractions, which we describe here through a series of examples. For reference throughout the following sections, the types and operations of the STM interface are:

Control.Concurrent.STM

data STM a -- abstract
instance Monad STM -- among other things

atomically :: STM a -> IO a

data TVar a -- abstract
newTVar   :: a -> STM (TVar a)
readTVar  :: TVar a -> STM a
writeTVar :: TVar a -> a -> STM ()

retry     :: STM a
orElse    :: STM a -> STM a -> STM a

throwSTM  :: Exception e => e -> STM a
catchSTM  :: Exception e => STM a -> (e -> STM a) -> STM a

Running Example: Managing Windows

Imagine a window manager that manages multiple desktops. The user can move windows from one desktop to another, while at the same time, a program can request that its own window move from its current desktop to another desktop. The window manager uses multiple threads: one to listen for input from the user, a set of threads to listen for requests from the programs ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Haskell High Performance Programming

Haskell High Performance Programming

Samuli Thomasson
Haskell Cookbook

Haskell Cookbook

Yogesh Sajanikar
Haskell in Depth

Haskell in Depth

Vitaly Bragilevsky

Publisher Resources

ISBN: 9781449335939Supplemental ContentErrata Page