Slide 1

Slide 1 text

CONCURRENCY FOR !DUMMIES (WHO DON’T GET IT(YET)) Monday, May 12, 14

Slide 2

Slide 2 text

CONCURRENCY FOR !DUMMIES (WHO DON’T GET IT(YET)) Monday, May 12, 14

Slide 3

Slide 3 text

Monday, May 12, 14

Slide 4

Slide 4 text

NIRD.US Monday, May 12, 14

Slide 5

Slide 5 text

ADA DEVELOPERS ACADEMY Monday, May 12, 14

Slide 6

Slide 6 text

Monday, May 12, 14

Slide 7

Slide 7 text

TYPICAL RESULT OF STUDYING CONCURRENCY Monday, May 12, 14

Slide 8

Slide 8 text

Monday, May 12, 14

Slide 9

Slide 9 text

• Ruby is bad at it • You don't need it • “HOLY S#!@ YOU HAVE TO DO THIS NOW!” • “HOLY S#!@ YOU GUYS DON'T SHARE STATE!” CONCURRENCY - WHAT HAVE YOU HEARD? Monday, May 12, 14

Slide 10

Slide 10 text

1 WHAT IS CONCURRENCY? Monday, May 12, 14

Slide 11

Slide 11 text

...I GUESS? Monday, May 12, 14

Slide 12

Slide 12 text

• roast turkey • cornbread stuffing • mashed potatoes & sweet potatoes • fresh baked pies • cooked cranberry relish • gravy made from the roast turkey drippings AMERICAN THANKSGIVING MENU Monday, May 12, 14

Slide 13

Slide 13 text

• Conflicting needs - multiple temperatures • Battling for a limited resource -- the one oven, the cooking range, or serving bowls • The need to interact with other dishes -- by arriving at the table at the same time, or because a side effect is an ingredient in another dish AMERICAN THANKSGIVING MENU Monday, May 12, 14

Slide 14

Slide 14 text

Monday, May 12, 14

Slide 15

Slide 15 text

Monday, May 12, 14

Slide 16

Slide 16 text

“CONCURRENCY != PARALLELISM” - EVERYONE WHO TALKS ABOUT CONCURRENCY Monday, May 12, 14

Slide 17

Slide 17 text

“CONCURRENCY IS ABOUT DEALING WITH LOTS OF THINGS AT ONCE. PARALLELISM IS ABOUT DOING LOTS OF THINGS AT ONCE.” -- ROB PIKE Monday, May 12, 14

Slide 18

Slide 18 text

2 CONCURRENCY ACCORDING TO RUBY Monday, May 12, 14

Slide 19

Slide 19 text

Process 1 Process 2 Process 3 Process 1 Thread 1 Thread 2 Thread 3 Thread 4 Monday, May 12, 14

Slide 20

Slide 20 text

Green Threads Global Interpreter Lock Ruby Interpreter (MRI) Kernel OS Thread Monday, May 12, 14

Slide 21

Slide 21 text

GLOBAL INTERPRETER LOCK (A.K.A. “THE GIL”) Monday, May 12, 14

Slide 22

Slide 22 text

def neverending_method i = 0 while i = rand(10) + i end end thread_0 = Thread.new{ neverending_method } thread_1 = Thread.new{ neverending_method } thread_2 = Thread.new{ neverending_method } thread_0.join Monday, May 12, 14

Slide 23

Slide 23 text

Monday, May 12, 14

Slide 24

Slide 24 text

Green Threads Global Interpreter Lock Ruby Interpreter (MRI) Kernel OS Threads Monday, May 12, 14

Slide 25

Slide 25 text

Monday, May 12, 14

Slide 26

Slide 26 text

Monday, May 12, 14

Slide 27

Slide 27 text

Monday, May 12, 14

Slide 28

Slide 28 text

Monday, May 12, 14

Slide 29

Slide 29 text

require 'benchmark' Benchmark.bm do |b| b.report('Sequential:') do 2.times do sleep(1) end end b.report('Threaded:') do a = Thread.new{ sleep(1) } b = Thread.new{ sleep(1) } a.join b.join end end Monday, May 12, 14

Slide 30

Slide 30 text

Monday, May 12, 14

Slide 31

Slide 31 text

require 'benchmark' Benchmark.bm do |b| b.report('Sequential:') do 10_000.times { 1 * 1 } end b.report('Threaded:') do a = Thread.new{ 5_000.times { 1 * 1 } } b = Thread.new{ 5_000.times { 1 * 1 } } a.join b.join end end Monday, May 12, 14

Slide 32

Slide 32 text

Monday, May 12, 14

Slide 33

Slide 33 text

sum = 0 threads = 10.times.map do Thread.new do 100_000.times do new_value = sum + 1 print "#{new_value} " if new_value % 250_000 == 0 sum = new_value end end end threads.each(&:join) puts "\nsum = #{sum}" Monday, May 12, 14

Slide 34

Slide 34 text

Monday, May 12, 14

Slide 35

Slide 35 text

sum = 0 threads = 10.times.map do Thread.new do 100_000.times do new_value = sum + 1 print "#{new_value} " if new_value % 250_000 == 0 sum = new_value end end end threads.each(&:join) puts "\nsum = #{sum}" Monday, May 12, 14

Slide 36

Slide 36 text

sum = 0 mutex = Mutex.new threads = 10.times.map do Thread.new do mutex.synchronize do 100_000.times do new_value = sum + 1 print "#{new_value} " if new_value % 250_000 == 0 sum = new_value end end end end threads.each(&:join) puts "\nsum = #{sum}" Monday, May 12, 14

Slide 37

Slide 37 text

Monday, May 12, 14

Slide 38

Slide 38 text

CLOJURE AGENTS & FUTURES SCALA ACTORS ERLANG ACTORS, SUPERVISORS, & GEN_SERVER RUBY THREADS & MUTEXES Monday, May 12, 14

Slide 39

Slide 39 text

“I WOULD REMOVE THE THREAD AND ADD ACTORS OR SOME OTHER MORE ADVANCED CONCURRENCY FEATURES” - MATZ Monday, May 12, 14

Slide 40

Slide 40 text

ACTORS Monday, May 12, 14

Slide 41

Slide 41 text

“DON’T COMMUNICATE BY SHARING STATE; SHARE STATE BY COMMUNICATING” Monday, May 12, 14

Slide 42

Slide 42 text

REACTORS Monday, May 12, 14

Slide 43

Slide 43 text

"WHICH IS BETTER, EMBER.JS OR ANGULAR?" Monday, May 12, 14

Slide 44

Slide 44 text

"WHICH IS BETTER, EMBER.JS OR ANGULAR? IF YOU LIKE EMBER, GO READ THESE THREE BLOGS, BUT IF YOU LIKE ANGULAR, BUILD ME A WEBSITE." Monday, May 12, 14

Slide 45

Slide 45 text

CHANNELS Monday, May 12, 14

Slide 46

Slide 46 text

4 SO NOW WHAT? Monday, May 12, 14

Slide 47

Slide 47 text

Monday, May 12, 14

Slide 48

Slide 48 text

TALK.JOIN • Concurrency can melt your brain • It behaves differently than you expect • It requires a different way of thinking • It needs new patterns and abstractions Monday, May 12, 14

Slide 49

Slide 49 text

HERE THERE BE DRAGONS • ~90% of Rubyists are building web apps • 80% of the time, concurrency isn’t the lowest hanging fruit • Its easy to overestimate the benefit, and underestimate the cost Monday, May 12, 14

Slide 50

Slide 50 text

IT IS EASY TO FEEL LIKE THIS Monday, May 12, 14

Slide 51

Slide 51 text

DON’T! GO WRITE SOME CODE INSTEAD! • Not knowing what you’re doing is no excuse • Start experimenting • Change your perspective and gain 80 IQ • Get started • Make a mess Monday, May 12, 14

Slide 52

Slide 52 text

RESOURCES • “Seven Concurrency Patterns in Seven Weeks” by Paul Butcher • jdantonio/concurrent-ruby -- Modern concurrency tools for Ruby. Inspired by Erlang, Clojure, Scala, Haskell, F#, C#, Java, and classic concurrency patterns. • “Go For Rubyists” by Trevor Bramble • 'Concurrency Is Not Parallelism (It's Better)’ by Rob Pike Monday, May 12, 14

Slide 53

Slide 53 text

KERRI MILLER (@KERRIZOR) • glass artist • Vespa mechanic • lighting designer • author & teacher • player of games • software developer Monday, May 12, 14

Slide 54

Slide 54 text

Monday, May 12, 14

Slide 55

Slide 55 text

...FIBERS... Monday, May 12, 14