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

Concurrency for !Dummies (Who Don't Get It (......

Concurrency for !Dummies (Who Don't Get It (...Yet))

You can’t go to a Ruby conference lately without someone banging on about concurrency and thread safety. You’re a smart Rubyist, but… you don’t get it. You understand the basic idea, you nod along, but once they get into the jargon, what does it all mean? How does it apply to you, and what is it, exactly? This talk will answer those questions, by engaging in a basic primer on concurrency in Ruby, a discussion of actual examples of how it works, and provide you with answers that will leave you nodding in agreement, rather than nodding off.

Kerri Miller

May 12, 2014
Tweet

More Decks by Kerri Miller

Other Decks in Programming

Transcript

  1. • 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
  2. • 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
  3. • 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
  4. “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
  5. Process 1 Process 2 Process 3 Process 1 Thread 1

    Thread 2 Thread 3 Thread 4 Monday, May 12, 14
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. CLOJURE AGENTS & FUTURES SCALA ACTORS ERLANG ACTORS, SUPERVISORS, &

    GEN_SERVER RUBY THREADS & MUTEXES Monday, May 12, 14
  13. “I WOULD REMOVE THE THREAD AND ADD ACTORS OR SOME

    OTHER MORE ADVANCED CONCURRENCY FEATURES” - MATZ Monday, May 12, 14
  14. "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
  15. 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
  16. 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
  17. 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
  18. 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
  19. KERRI MILLER (@KERRIZOR) • glass artist • Vespa mechanic •

    lighting designer • author & teacher • player of games • software developer Monday, May 12, 14