Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Let's talk concurrency

Plataformatec
September 21, 2012

Let's talk concurrency

For a long time, the de facto way of doing multi-core concurrency was using threads. However, the complexity of manipulating threads and state affected negatively how developers perceive concurrency. Fortunately, languages like Clojure and Erlang implement new paradigms that aim to make concurrency easier.

In this talk, José Valim is going to discuss the role state play in concurrency and introduce different paradigms for multi-core concurrency, like Actors and Software Transactional Memory, explaining their trade-offs and why they matter to us developers.

Plataformatec

September 21, 2012
Tweet

More Decks by Plataformatec

Other Decks in Technology

Transcript

  1. factorial = lambda do |x| case x when 0 1

    else x * factorial.(x-1) end end print factorial.(10) # => 3628800 Friday, September 21, 2012
  2. lambda do |a, b| c = expensive_function.(a) d = also_expensive.(b)

    c + d end a = 1 Friday, September 21, 2012
  3. lambda do |1, b| c = 42 d = also_expensive.(b)

    c + d end Friday, September 21, 2012
  4. l = lambda { |a,b,c| a + b + c

    } l.(1, 2, 3) #=> 6 l.curry #=> #<Proc> l.curry.(1) #=> #<Proc> l.curry.(1).(2).(3) #=> 6 Currying Friday, September 21, 2012
  5. l = lambda { |a,b| a * b } double

    = l.curry.(2) triple = l.curry.(3) Currying Friday, September 21, 2012
  6. sum a b c = a + b + c

    (sum 1 2 3) (((sum 1) 2) 3) Currying Friday, September 21, 2012
  7. x = expensive(1) y = expensive(3) z = (sum x)

    ((z 2) y) Friday, September 21, 2012
  8. x = expensive(1) y = expensive(3) z = (sum x)

    ((z 2) y) Compiler Friday, September 21, 2012
  9. x = expensive(1) y = expensive(3) z = (sum x)

    ((z 2) y) Compiler (sum expensive(1) 2 expensive(3)) Friday, September 21, 2012
  10. lambda do |a, b| thread { c = expensive_function.(a) }

    thread { d = also_expensive.(b) } c + d end Friday, September 21, 2012
  11. main spawn thread 1 spawn thread 2 unbound c defines

    c unbound d Friday, September 21, 2012
  12. main spawn thread 1 spawn thread 2 unbound c defines

    c unbound d defines d Friday, September 21, 2012
  13. main spawn thread 1 spawn thread 2 unbound c defines

    c unbound d defines d c + d Friday, September 21, 2012
  14. class Counter mattr_accessor :i self.i = 0 end thread {

    Counter.i = Counter.i + 1 } Friday, September 21, 2012
  15. thread 1 Counter.i thread 2 0 1 0 1 1

    Friday, September 21, 2012
  16. thread 1 Counter.i thread 2 0 1 0 1 1

    1 Friday, September 21, 2012
  17. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 Friday, September 21, 2012
  18. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 1 Friday, September 21, 2012
  19. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 1 2 Friday, September 21, 2012
  20. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 1 2 2 Friday, September 21, 2012
  21. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 1 2 2 2 Friday, September 21, 2012
  22. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 1 2 2 2 2 Friday, September 21, 2012
  23. class Counter mattr_accessor :i self.i = 0 end thread {

    Counter.i = Counter.i + 1 } Friday, September 21, 2012
  24. class Counter mattr_accessor :i self.i = 0 end thread {

    synchronize { Counter.i = Counter.i + 1 } } Friday, September 21, 2012
  25. thread 1 Counter.i thread 2 0 1 0 1 1

    Friday, September 21, 2012
  26. thread 1 Counter.i thread 2 0 1 0 1 1

    1 synchronize { Friday, September 21, 2012
  27. thread 1 Counter.i thread 2 0 1 0 1 1

    2 1 synchronize { Friday, September 21, 2012
  28. thread 1 Counter.i thread 2 0 1 0 1 1

    2 1 synchronize { 2 } Friday, September 21, 2012
  29. thread 1 Counter.i thread 2 0 1 0 1 1

    2 2 1 synchronize { 2 } Friday, September 21, 2012
  30. thread 1 Counter.i thread 2 0 1 0 1 1

    2 2 2 1 synchronize { 2 } Friday, September 21, 2012
  31. thread 1 Counter.i thread 2 0 1 0 1 1

    2 2 3 2 1 synchronize { 2 } Friday, September 21, 2012
  32. thread 1 Counter.i thread 2 0 1 0 1 1

    2 2 3 3 2 1 synchronize { 2 } Friday, September 21, 2012
  33. class Counter mattr_accessor :i self.i = ref { 0 }

    end thread { atomic { Counter.i = Counter.i + 1 } } Friday, September 21, 2012
  34. thread 1 Counter.i thread 2 0 1 0 1 1

    Friday, September 21, 2012
  35. thread 1 Counter.i thread 2 0 1 0 1 1

    1 atomic { Friday, September 21, 2012
  36. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 atomic { Friday, September 21, 2012
  37. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 1 atomic { Friday, September 21, 2012
  38. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 1 atomic { Friday, September 21, 2012
  39. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 1 atomic { Friday, September 21, 2012
  40. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 1 atomic { Friday, September 21, 2012
  41. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 1 atomic { } Friday, September 21, 2012
  42. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 1 atomic { } Friday, September 21, 2012
  43. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 1 atomic { } Friday, September 21, 2012
  44. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 2 1 atomic { } Friday, September 21, 2012
  45. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 2 2 1 atomic { } Friday, September 21, 2012
  46. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 2 2 3 1 atomic { } Friday, September 21, 2012
  47. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 2 2 3 3 1 atomic { } Friday, September 21, 2012
  48. server = lambda do |i| receive when :increment server.(i+1) when

    :check client <- i server.(i) else warn "unknown message" server.(i) end end server.(0) Friday, September 21, 2012
  49. thread 1 server thread 2 0 2 1 :increment :increment

    :increment Friday, September 21, 2012
  50. thread 1 server thread 2 0 2 1 3 :increment

    :increment :increment Friday, September 21, 2012
  51. thread 1 server thread 2 0 2 1 3 :increment

    :increment :increment :check Friday, September 21, 2012
  52. thread 1 server thread 2 0 2 1 3 3

    :increment :increment :increment :check Friday, September 21, 2012
  53. thread 1 server thread 2 0 2 1 3 3

    :increment :increment :increment :check Friday, September 21, 2012
  54. thread 1 server thread 2 0 2 1 3 3

    3 :increment :increment :increment :check Friday, September 21, 2012
  55. “Do not communicate by sharing memory; instead, share memory by

    communicating;” Effective Go Friday, September 21, 2012