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

Yet another introduction to concurrency

1703ab65a84c2d8d5f286f1c8e336d92?s=47 Nando Sousa
September 24, 2016

Yet another introduction to concurrency

Slides of my talk for RubyconfBR 2016

1703ab65a84c2d8d5f286f1c8e336d92?s=128

Nando Sousa

September 24, 2016
Tweet

Transcript

  1. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu yet another

    introduction to CONCURRENCY
  2. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Nando Sousa

    Software Developer @ Resultados Digitais @nandosousafr
  3. Fun fact about me..

  4. Ship It! http://bit.ly/2d3BET8 rdstation.com.br shipit.resultadosdigitais.com.br

  5. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu SO… LET’S

    ABOUT CONCURRENCY…
  6. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Why this

    is hot topic these days? MULTICORE CRISIS
  7. Resultados Digitais: We're hiring! What’s Moore’s law? processing power will

    double every 18 months
  8. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu MULTICORE

  9. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu The free

    lunch is over! http://www.gotw.ca/publications/concurrency-ddj.htm
  10. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Parallelism Concurrency

    vs
  11. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu “Concurrency is

    about dealing with lots of things at once. Parallelism is about doing lots of things at once.” Rob Pike Co-creator of Golang
  12. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu “Concurrency: Programming

    as the composition of independently executing processes.” Rob Pike Co-creator of Golang
  13. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu “The world

    is concurrent!” Rob Pike Co-creator of Golang
  14. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Let’s see

    some code!
  15. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu require 'concurrent'

    require 'httparty' USERS = [ 'nandosousafr', 'jeanmantheussouto', 'thiesen', 'pcasaretto', 'hugoluchessi', 'jeanholderbaum', 'aureliosaraiva' ] def get_followers_count(user) endpoint_url = “http://api.github.com/users/#{user}/ followers” response = HTTParty.get(endpoint_url) raise unless response.status == 200 response.count end def execute_concurrently futures = USERS.collect do |user| Concurrent::Future.execute { get_followers_count(user) } end futures.collect {|future| future.value } end
  16. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu def execute_concurrently

    futures = USERS.collect do |user| Concurrent::Future.execute { get_followers_count(user) } end futures.collect {|future| future.value } end composition independently executing processes
  17. Resultados Digitais: We're hiring! CONCURRENCY VS PARALLELISM • Concurrency is

    about design; • Parallelism is about optimization; • Different, but related;
  18. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Shared memory

    concurrent model
  19. Resultados Digitais: We're hiring! SHARED MEMORY CONCURRENT MODEL • Languages

    like (Ruby, Java and C++) supports it; • Traditional; • A little for than a abstraction what the OS does;
  20. Resultados Digitais: We're hiring! Thread OS Process Thread Thread Shared

    Memory Space
  21. Resultados Digitais: We're hiring! 1 var_a = "foo" 2 #=>

    foo 3 4 var_b = var_a 5 #=> foo 6 7 var_b.upcase! 8 #=> FOO 9 10 var_a 11 #=> FOO SHARED MEMORY CONCURRENT MODEL
  22. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Race Condition

  23. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu

  24. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu 1 public

    class Counting { 2 public static void main(String[] args) throws InterruptedException { 3 class Counter { 4 private int count = 0; 5 public void increment() { ++count; } 6 public int getCount() { return count; } 7 } 8 9 final Counter counter = new Counter(); 10 11 class CountingThread extends Thread { 12 public void run() { 13 for(int x = 0; x < 10000; ++x) 14 counter.increment(); 15 16 } 17 } 18 19 CountingThread t1 = new CountingThread(); 20 CountingThread t2 = new CountingThread(); 21 22 t1.start(); t2.start(); 23 t1.join(); t2.join(); 24 25 System.out.println(counter.getCount()); 26 } 27 }
  25. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu $ seq

    5 | xargs -L 1 java Counting 14925 10726 10850 12582 17219
  26. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu $ seq

    5 | xargs -L 1 java Counting 14925 10726 10850 12582 17219
  27. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu $ seq

    5 | xargs -L 1 java Counting 14925 10726 10850 12582 17219
  28. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu $ seq

    5 | xargs -L 1 java Counting 14925 10726 10850 12582 17219
  29. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu “A race

    condition is an undesirable situation that occurs when a device or system attempts to perform two or more operations at the same time, but because of the nature of the device or system, the operations must be done in the proper sequence to be done correctly.” http://searchstorage.techtarget.com/definition/race-condition
  30. Resultados Digitais: We're hiring! INCREMENT METHOD READ MODIFY WRITE

  31. Resultados Digitais: We're hiring! ++COUNT READ MODIFY WRITE 42 42

    ++ ++ 43 43
  32. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Mutex (

    Mutual Exclusion ) Using locks to ensure that only one thread can access data at a time.
  33. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu 1 public

    class Counting { 2 public static void main(String[] args) throws InterruptedException { 3 class Counter { 4 private int count = 0; 5 public synchronized void increment() { ++count; } 6 public int getCount() { return count; } 7 } 8 9 final Counter counter = new Counter(); 10 11 class CountingThread extends Thread { 12 public void run() { 13 for(int x = 0; x < 10000; ++x) 14 counter.increment(); 15 16 } 17 } 18 19 CountingThread t1 = new CountingThread(); 20 CountingThread t2 = new CountingThread(); 21 22 t1.start(); t2.start(); 23 t1.join(); t2.join(); 24 25 System.out.println(counter.getCount()); 26 } 27 }
  34. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu $ seq

    5 | xargs -L 1 java Counting 20000 20000 20000 20000 20000
  35. Resultados Digitais: We're hiring! RECAP - SHARED MEMORY CONCURRENT MODEL

    • Locks • Deadlocks • Communication;
  36. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Message Passing

    Concurrent Model
  37. Resultados Digitais: We're hiring! Thread SHARED MEMORY ENVIRONMENT Thread Thread

    Shared Memory
  38. Resultados Digitais: We're hiring! Process Erlang VM Process Process Process

    Process Process
  39. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu “Do not

    communicate by sharing memory; instead, share memory by communicating.” Rob Pike
  40. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Let’s see

    some code!
  41. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu 1 defmodule

    Counter do 2 def start(count) do 3 spawn(__MODULE__, :loop, [count]) 4 end 5 6 def next(counter) do 7 send(counter, {:next}) 8 end 9 10 def loop(count) do 11 receive do 12 {:next} -> 13 IO.puts("Current count is: #{count}") 14 loop(count + 1) 15 end 16 end 17 end
  42. Resultados Digitais: We're hiring! $ iex iex(3)> counter = Counter.start(42)

    #PID<0.63.0> iex(4)> Counter.next(counter) Current count is: 42 {:next} iex(5)> Counter.next(counter) Current count is: 43 {:next} iex(6)> Counter.next(counter) Current count is: 44 {:next}
  43. Resultados Digitais: We're hiring! MESSAGE PASSING CONCURRENT MODEL • Enforces

    you to write concurrent software; • No locks;
  44. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Other resources:

    Concurrency is not parallelism - by Rob Pike Everything You Know About the GIL is Wrong by Jerry D'Antonio The Free Lunch Is Over by Herb Sutter
  45. Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Thanks Questions?

    @nandosousafr