Clojure’s                            Software                            Transactional                            Memory  ...
what’s it good for?Thursday, December 15, 11
it’s pretty much useless                                    unless you have:                                    concurrenc...
the pitch                                    “It allows you to do                                    coordinated change   ...
kind of like                            database transactions.                                but in memory.Thursday, Dece...
example              shared state      1   9       5   7       3   10       4                                             ...
user=> (def my-atom (atom 0))                            #user/my-atom                                                    ...
broken example                              (using atoms)Thursday, December 15, 11
changing state                            atomic   (acts as one point in time)                            consistent    (f...
sharing state                            mutate in                            STM only!                                   ...
STM functions                               ref    reference to a collection                             dosync   transact...
working and slightly                              different example                                            (using STM)...
STM functions                               ref    reference to a collection                             dosync   transact...
what to think about                            is it okay if others make changes in parallel?                             ...
how does it work?                            MVCC, snapshot isolation                            persistent data structure...
Upcoming SlideShare
Loading in …5
×

Introduction to Clojure's STM

4,887 views

Published on

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,887
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
21
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Introduction to Clojure's STM

  1. 1. Clojure’s Software Transactional Memory @fronx at @cljugb 12/2011Thursday, December 15, 11
  2. 2. what’s it good for?Thursday, December 15, 11
  3. 3. it’s pretty much useless unless you have: concurrency shared state changes to stateThursday, December 15, 11
  4. 4. the pitch “It allows you to do coordinated change without the complexity of locking.” — Rich Hickey http://www.infoq.com/interviews/hickey-clojureThursday, December 15, 11
  5. 5. kind of like database transactions. but in memory.Thursday, December 15, 11
  6. 6. example shared state 1 9 5 7 3 10 4 2 6 8 changing state 4 4 threads a b c d e solution: do it atomically!Thursday, December 15, 11
  7. 7. user=> (def my-atom (atom 0)) #user/my-atom atoms (No STM here.) user=> @my-atom 0 user=> (swap! my-atom inc) 1 user=> @my-atom 1 user=> (swap! my-atom (fn [n] (* (+ n n) 2))) 4Thursday, December 15, 11
  8. 8. broken example (using atoms)Thursday, December 15, 11
  9. 9. changing state atomic (acts as one point in time) consistent (from valid state to valid state) isolated (changes are local until committed)Thursday, December 15, 11
  10. 10. sharing state mutate in STM only! coordinated independent synchronous ref atom asynchronous – agent receive message and returnThursday, December 15, 11
  11. 11. STM functions ref reference to a collection dosync transaction set-ref set new value alter set new value via a function commute set new value and don’t block ensure block writes by others io! IllegalStateExceptionThursday, December 15, 11
  12. 12. working and slightly different example (using STM)Thursday, December 15, 11
  13. 13. STM functions ref reference to a collection dosync transaction set-ref set new value alter set new value via a function commute set new value and don’t block ensure block writes by others io! IllegalStateExceptionThursday, December 15, 11
  14. 14. what to think about is it okay if others make changes in parallel? commute multi-ref constraints? ensureThursday, December 15, 11
  15. 15. how does it work? MVCC, snapshot isolation persistent data structures on-commit evaluation http://java.ociweb.com/mark/stm/article.htmlThursday, December 15, 11

×