Introduction

What is STM?

Software transactional memory is a mediator that sits between a critical section of your code (the atomic block) and the program’s heap. The STM intervenes during reads and writes in the atomic block, allowing it to check and/or avoid interference other threads. If the loads and stores of multiple threads have gotten interleaved, then all of the writes of the atomic block are rolled back and the entire block is retried. If the accesses by the critical section are not interleaved, then it is as if they were done atomically and the atomic block can be committed. Other threads or actors can only see committed changes.

STMs use optimistic concurrency control. They optimistically assume that atomic blocks will be able to run in parallel, and then back up and retry if that speculation is incorrect. Keeping the old versions of data so that you can back up imposes some overhead, but optimistic concurrency typically has better scalability than alternative approaches.

ScalaSTM – no magic

There have been several ambitious attempts to create STMs that can run existing sequential imperative code in parallel. This is a difficult task that requires a lot of magic, because calls to the STM need to be inserted for every load and store of a non-final field or array element inside an atomic block. Good performance is also difficult because of the large numbers of reads and writes.

The ScalaSTM API avoids the need for magic by only managing Ref-s. This means that there are fewer memory locations to manage, and no bytecode instrumentation or compiler modifications are required. As in Haskell and Clojure, the usefulness of Ref is multiplied by the language’s good support for immutable data structures. ScalaSTM also includes concurrent sets and maps that can be used in transactions.

Who is ScalaSTM for?

ScalaSTM is for programmers whose threads or actors need to coordinate access to shared data. In a server, this might be the list of active connections or a cache. In a client, this might be a partial result or worker thread status.

Pros

Cons