SlideShare a Scribd company logo
Software Transactional Memory
Steve Severance – Alpha Heavy Industries
Motivation
• Innovations in concurrency has not kept pace
• Still using locks
• Immutability has helped in Haskell
STM Basics
• Transactional Concurrency
• Atomicity – Results are visible all at once
• Isolation – Action unaffected by other threads
• Everything you need is in the stm-2.4.2
package
Haskell and STM
• Exploits Three Haskell Features:
– Purity (Transactions must be pure)
– Monads
– Immutability (Values are never modified)
• Utilizes the Type System
– Anything involved in the transaction is marked
(TVar, TChan, STM, etc…)
Basic Transaction Pattern
• atomically $ do …
• atomically evaluates the transaction
main :: IO ()
main = do
var <- newTVarIO False
atomically $ do
val <- readTVar var
if val == False then writeTVar var True else return ()
Primitives
• TVar (Always has a Value)
• TMVar (Analogous to Regular MVar)
• TChan (Multicast Capabilities)
• TQueue (FIFO Queue)
• TBQueue (Bounded Queue)
• TSem (Semaphore)
Retrying
• Use the retry function to retry the transaction
• The transaction will only be run when one of
the inputs has changed
withdrawFunds :: TVar Account -> Money -> STM Money
withdrawFunds accountVar amount = do
account <- readTVar accountVar
if (aBalance account) > amount
then
writeTVar accountVar account{aBalance = (aBalance account) `subtract` amount} >> return amount
else retry
Choices
• orElse runs a second option if the first retrys
withdrawEvil :: TVar Account -> TChan Account -> Money -> STM Money
withdrawEvil accountVar nsfChan amount = do
withdrawFunds accountVar amount
`orElse` withdrawNonSufficientFunds accountVar nsfChan amount
Using Alternative
• You always wanted to use those cool
operators
• Use <|> instead of orElse
Dealing with Exceptions
• Revisiting the Atomic Guarantee
– No need for rollback unlike MVar and Chan
• STM can throw and catch
Bounded Queues
• TBQueue limits its size
• Writers will block until space becomes
available
• Useful for performance and memory
management
Being Strict
• Use NFData (deepseq)
• We internally use special write functions to
keep work from leaking across thread
boundaries
• Transaction TimewriteTVar’ :: NFData a => TVar a -> a -> STM ()
{-# INLINE writeTVar' #-}
writeTVar' var val =
rnf val `seq` writeTVar var val
Other Languages
• Clojure
• Scala
• C++
• C#/.Net
References
• Composable Memory Transactions
• Beautiful Concurrency
• STM Retrospective for .Net
• Gists:
– Sample 1

More Related Content

PDF
Towards JVM Dynamic Languages Toolchain
PDF
Seven Ineffective Coding Habits of Many Programmers
PDF
Distributed Consensus A.K.A. "What do we eat for lunch?"
PDF
A dive into akka streams: from the basics to a real-world scenario
PDF
BDD - Writing better scenario
PPTX
Stm talk 2016-03-09
ODP
The free lunch is over
PPTX
Concurrency Constructs Overview
Towards JVM Dynamic Languages Toolchain
Seven Ineffective Coding Habits of Many Programmers
Distributed Consensus A.K.A. "What do we eat for lunch?"
A dive into akka streams: from the basics to a real-world scenario
BDD - Writing better scenario
Stm talk 2016-03-09
The free lunch is over
Concurrency Constructs Overview

Similar to STM in Haskell (20)

PPT
Deuce STM - CMP'09
PPTX
Pellucid stm
PDF
Implementing STM in Java
ODP
Software Transactioneel Geheugen
PPT
BayFP: Concurrent and Multicore Haskell
PPT
Clojure concurrency
PDF
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
PDF
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
PDF
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
PDF
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
PDF
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
PDF
A Survey of Concurrency Constructs
PPTX
Concurrency in Programming Languages
PDF
Quick into to Software Transactional Memory in Frege
PPTX
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
PDF
Persistent Data Structures by @aradzie
PDF
Atomically { Delete Your Actors }
PDF
Akka: Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Ac...
PPTX
Concurrency: how to shoot yourself in both feet. Simultaneously.
PDF
Functional Programming with Immutable Data Structures
Deuce STM - CMP'09
Pellucid stm
Implementing STM in Java
Software Transactioneel Geheugen
BayFP: Concurrent and Multicore Haskell
Clojure concurrency
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
A Survey of Concurrency Constructs
Concurrency in Programming Languages
Quick into to Software Transactional Memory in Frege
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Persistent Data Structures by @aradzie
Atomically { Delete Your Actors }
Akka: Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Ac...
Concurrency: how to shoot yourself in both feet. Simultaneously.
Functional Programming with Immutable Data Structures
Ad

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
cuic standard and advanced reporting.pdf
PPTX
Cloud computing and distributed systems.
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Electronic commerce courselecture one. Pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Empathic Computing: Creating Shared Understanding
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
KodekX | Application Modernization Development
Mobile App Security Testing_ A Comprehensive Guide.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Encapsulation_ Review paper, used for researhc scholars
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
cuic standard and advanced reporting.pdf
Cloud computing and distributed systems.
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectroscopy.pptx food analysis technology
Machine learning based COVID-19 study performance prediction
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Chapter 3 Spatial Domain Image Processing.pdf
MIND Revenue Release Quarter 2 2025 Press Release
The AUB Centre for AI in Media Proposal.docx
Electronic commerce courselecture one. Pdf
MYSQL Presentation for SQL database connectivity
Empathic Computing: Creating Shared Understanding
Diabetes mellitus diagnosis method based random forest with bat algorithm
Ad

STM in Haskell

  • 1. Software Transactional Memory Steve Severance – Alpha Heavy Industries
  • 2. Motivation • Innovations in concurrency has not kept pace • Still using locks • Immutability has helped in Haskell
  • 3. STM Basics • Transactional Concurrency • Atomicity – Results are visible all at once • Isolation – Action unaffected by other threads • Everything you need is in the stm-2.4.2 package
  • 4. Haskell and STM • Exploits Three Haskell Features: – Purity (Transactions must be pure) – Monads – Immutability (Values are never modified) • Utilizes the Type System – Anything involved in the transaction is marked (TVar, TChan, STM, etc…)
  • 5. Basic Transaction Pattern • atomically $ do … • atomically evaluates the transaction main :: IO () main = do var <- newTVarIO False atomically $ do val <- readTVar var if val == False then writeTVar var True else return ()
  • 6. Primitives • TVar (Always has a Value) • TMVar (Analogous to Regular MVar) • TChan (Multicast Capabilities) • TQueue (FIFO Queue) • TBQueue (Bounded Queue) • TSem (Semaphore)
  • 7. Retrying • Use the retry function to retry the transaction • The transaction will only be run when one of the inputs has changed withdrawFunds :: TVar Account -> Money -> STM Money withdrawFunds accountVar amount = do account <- readTVar accountVar if (aBalance account) > amount then writeTVar accountVar account{aBalance = (aBalance account) `subtract` amount} >> return amount else retry
  • 8. Choices • orElse runs a second option if the first retrys withdrawEvil :: TVar Account -> TChan Account -> Money -> STM Money withdrawEvil accountVar nsfChan amount = do withdrawFunds accountVar amount `orElse` withdrawNonSufficientFunds accountVar nsfChan amount
  • 9. Using Alternative • You always wanted to use those cool operators • Use <|> instead of orElse
  • 10. Dealing with Exceptions • Revisiting the Atomic Guarantee – No need for rollback unlike MVar and Chan • STM can throw and catch
  • 11. Bounded Queues • TBQueue limits its size • Writers will block until space becomes available • Useful for performance and memory management
  • 12. Being Strict • Use NFData (deepseq) • We internally use special write functions to keep work from leaking across thread boundaries • Transaction TimewriteTVar’ :: NFData a => TVar a -> a -> STM () {-# INLINE writeTVar' #-} writeTVar' var val = rnf val `seq` writeTVar var val
  • 13. Other Languages • Clojure • Scala • C++ • C#/.Net
  • 14. References • Composable Memory Transactions • Beautiful Concurrency • STM Retrospective for .Net • Gists: – Sample 1