Using Software Transactional Memory
In this section, we will study the basics of using STM. Historically, multiple STM implementations were introduced for Scala and the JVM platform. The particular STM implementation described in this chapter is called ScalaSTM. There are two reasons that ScalaSTM is our STM of choice. First, ScalaSTM was authored by a group of STM experts who agreed on a standardized set of APIs and features. Future STM implementations for Scala are strongly encouraged to implement these APIs. Second, the ScalaSTM API is designed for multiple STM implementations, and comes with an efficient default implementation. Different STM implementations can be chosen when the program starts. Users can write applications using a standardized API, and seamlessly switch to a different STM implementation later.
The atomic
statement is a fundamental abstraction at the core of every STM. When the program executes a block of code marked with the atomic
symbol, it starts a memory transaction...