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. 2.

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

    Software Developer @ Resultados Digitais @nandosousafr
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 17.

    Resultados Digitais: We're hiring! CONCURRENCY VS PARALLELISM • Concurrency is

    about design; • Parallelism is about optimization; • Different, but related;
  8. 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;
  9. 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
  10. 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 }
  11. 25.

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

    5 | xargs -L 1 java Counting 14925 10726 10850 12582 17219
  12. 26.

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

    5 | xargs -L 1 java Counting 14925 10726 10850 12582 17219
  13. 27.

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

    5 | xargs -L 1 java Counting 14925 10726 10850 12582 17219
  14. 28.

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

    5 | xargs -L 1 java Counting 14925 10726 10850 12582 17219
  15. 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
  16. 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.
  17. 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 }
  18. 34.

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

    5 | xargs -L 1 java Counting 20000 20000 20000 20000 20000
  19. 35.
  20. 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
  21. 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
  22. 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}
  23. 43.
  24. 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