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

Vamos falar de Concorrência

Vamos falar de Concorrência

Por José Valim, na RubyConf Brasil 2012.

Plataformatec

August 31, 2012
Tweet

More Decks by Plataformatec

Other Decks in Programming

Transcript

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

    else x * factorial.(x-1) end end print factorial.(10) # => 3628800
  2. •não existe random() •não existe I/O em disco •não existem

    efeitos colaterais •sempre o mesmo resultado
  3. 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
  4. l = lambda { |a,b| a * b } double

    = l.curry.(2) triple = l.curry.(3) Currying
  5. mult a b = a * b double = mult

    2 (mult 2 3) ((mult 2) 3) Currying haskell
  6. mult a b = a * b double = mult

    2 double 3 haskell
  7. mult a b = a * b double = mult

    2 double 3 Compilador haskell
  8. mult a b = a * b double = mult

    2 double 3 Compilador (mult 2 3) haskell
  9. lambda do |a, b| thread { c = expensive_function.(a) }

    thread { d = also_expensive.(b) } c + d end
  10. class Counter mattr_accessor :i self.i = 0 end thread {

    synchronize { Counter.i = Counter.i + 1 } }
  11. thread 1 Counter.i thread 2 0 1 0 1 1

    2 2 1 synchronize { 2 }
  12. thread 1 Counter.i thread 2 0 1 0 1 1

    2 2 2 1 synchronize { 2 }
  13. thread 1 Counter.i thread 2 0 1 0 1 1

    2 2 3 2 1 synchronize { 2 }
  14. thread 1 Counter.i thread 2 0 1 0 1 1

    2 2 3 3 2 1 synchronize { 2 }
  15. class Counter mattr_accessor :i self.i = ref { 0 }

    end thread { atomic { Counter.i = Counter.i + 1 } }
  16. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 1 atomic { }
  17. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 1 atomic { }
  18. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 1 atomic { }
  19. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 2 1 atomic { }
  20. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 2 2 1 atomic { }
  21. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 2 2 3 1 atomic { }
  22. thread 1 Counter.i thread 2 0 1 0 1 1

    1 1 2 2 2 2 2 3 3 1 atomic { }
  23. 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)
  24. client 1 server client 2 0 2 1 3 :increment

    :increment :increment :check
  25. client 1 server client 2 0 2 1 3 3

    :increment :increment :increment :check
  26. client 1 server client 2 0 2 1 3 3

    :increment :increment :increment :check
  27. client 1 server client 2 0 2 1 3 3

    3 :increment :increment :increment :check