Share
Embed
Yet another introduction to concurrency
Slides of my talk for RubyconfBR 2016
Nando Sousa
September 24, 2016
Transcript
45 slides
-
Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu yet another
introduction to CONCURRENCY -
Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Nando Sousa
Software Developer @ Resultados Digitais @nandosousafr -
Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu Why this
is hot topic these days? MULTICORE CRISIS -
Resultados Digitais: We're hiring! What’s Moore’s law? processing power will
double every 18 months -
Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu The free
lunch is over! http://www.gotw.ca/publications/concurrency-ddj.htm -
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 -
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 -
Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu “The world
is concurrent!” Rob Pike Co-creator of Golang -
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 -
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 -
Resultados Digitais: We're hiring! CONCURRENCY VS PARALLELISM • Concurrency is
about design; • Parallelism is about optimization; • Different, but related; -
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; -
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 -
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 } -
Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu $ seq
5 | xargs -L 1 java Counting 14925 10726 10850 12582 17219 -
Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu $ seq
5 | xargs -L 1 java Counting 14925 10726 10850 12582 17219 -
Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu $ seq
5 | xargs -L 1 java Counting 14925 10726 10850 12582 17219 -
Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu $ seq
5 | xargs -L 1 java Counting 14925 10726 10850 12582 17219 -
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 -
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. -
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 } -
Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu $ seq
5 | xargs -L 1 java Counting 20000 20000 20000 20000 20000 -
Resultados Digitais: We're hiring! RECAP - SHARED MEMORY CONCURRENT MODEL
• Locks • Deadlocks • Communication; -
Resultados Digitais: We're hiring! Download slides at: bit.ly/2cswCwu “Do not
communicate by sharing memory; instead, share memory by communicating.” Rob Pike -
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 -
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} -
Resultados Digitais: We're hiring! MESSAGE PASSING CONCURRENT MODEL • Enforces
you to write concurrent software; • No locks; -
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