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

Play with ruby threads

Play with ruby threads

Damien Mathieu

February 12, 2014
Tweet

More Decks by Damien Mathieu

Other Decks in Programming

Transcript

  1. require 'net/http' ! t1 = Thread.new { ! content =

    Net::HTTP.get(URI.parse("http://google.com")) puts "I just fetched google.com" } ! t2 = Thread.new { ! content = Net::HTTP.get(URI.parse("http://github.com")) puts "I just fetched github.com" } ! t1.join && t2.join puts "I've finished executing everything"
  2. › time ruby threads.rb! I just fetched google.com! I just

    fetched github.com! I've finished executing everything! ruby threads.rb 0.13s user 0.06s system 40% cpu 0.450 total
  3. require 'net/http' ! content = Net::HTTP.get(URI.parse("http://google.com")) puts "I just fetched

    google.com" ! content = Net::HTTP.get(URI.parse("http://github.com")) puts "I just fetched github.com" ! puts "I've finished executing everything"
  4. time ruby no_threads.rb! I just fetched google.com! I just fetched

    github.com! I've finished executing everything! ruby no_thread.rb 0.12s user 0.06s system 6% cpu 2.698 total
  5. Thread.new • Thread.new { … }! • Thread.fork { …

    }! • Thread.start(1, 2) { |x, y| … }
  6. Thread#status thread = Thread.new do Thread.current.status #=> 'run' 2 *

    3 end puts thread.status #=> 'run' thread.join puts thread.status #=> false
  7. Thread#stop thread = Thread.new do Thread.stop puts 'Hello there' end

    ! # wait for the thread to trigger its stop nil until thread.status == 'sleep' thread.wakeup thread.join
  8. Non-deterministic context switching • In order to provide fair access,

    the thread scheduler can pause a thread at any time
  9. Non-deterministic context switching class Post def initialize @comments_mutex = Mutex.new

    end ! def comments @comments_mutex.synchronize do @comments ||= [] end end end
  10. MRI’s GIL MRI has something called a global interpreter lock

    (GIL).! It's a lock around the execution of Ruby code.! This means that in a multi-threaded context, only one thread can execute Ruby code at any one time.
  11. MRI’s GIL So if you have 8 threads busily working

    on a 8-core machine, only one thread and one core will be busy at any given time.! The GIL exists to protect Ruby internals from race conditions that could corrupt data.! There are caveats and optimizations, but this is the gist.
  12. How many threads should I use? • Is your program

    IO or CPU bound?! • IO Bound will allow a lot of threads! • CPU Bound will depend of the number of cores! • The only way is to measure