Software Transactional Memory

Steve Severance
has used Software Transactional Memory (STM) pretty intensively in trading and web crawling applications. He shares things he’s learned, some gotchas, and a comparison of the way STM works in Haskell vs other languages.

STM in Haskell

Summary

  • Motivation for STM
  • CPUs are getting more cores yet our tools remain fairly primitive

    • Locks and lock convoys
    • Dealing with deadlocks
  • Immutability has helped Haskell some with this
  • What is STM? It is in-memory transactional concurrency that gives us two properties

    • Atomicity: all results of your transaction will be visible at once
    • Isolation of underlying primitives
  • Haskell is the best language in the world in which to implement an STM

    • It was originally 100 lines of code, and even now it’s only about 600 lines of C!
    • It exploits three properties of the language: purity, immutability and laziness
    • Learning about STM was when Steve first started to appreciate laziness; up until that point laziness had only hurt, never helped
  • Haskell retries an STM transaction as needed. Immutability allows it to do this fearlessly, and laziness avoids fully evaluating the transaction
  • STM is harder in other languages. For instance in Clojure you have to manually implement rollback functionality.
  • There are some helpful auxiliary packages such as stm-chans
  • How about performing unsafe IO in the STM monad? Never! If you’re wanting to do this you’re “doing it wrong.”
  • Discussion of the primitives TVar, TMVar, TChan, TQueue, TBQueue, TSem
  • How the retry
    function works by putting our thread to sleep
  • Using orElse and the Alternative typeclass
  • How often does a transaction block get re-run? Does it get inefficient? Use the stm-stats
    package to see how well your transactions are performing.
  • The asyc
    package internally uses STM and has great integration with it
  • Other languages have STM of varying convenience. Clojure, Scala, C++, C#

    • The C# team spent about three years trying to write an STM. They gave up and wrote a retrospective
      on why it was so hard.
    • Side effects make STM hellish in languages other than Haskell.
  • Q&A
稿源:Begriffs.com blog (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Software Transactional Memory

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录