Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Threading and Concurrency in Ruby

1,400 views

Published on

Rubyists generally tend to shy away from multi-threaded code.
Cognitive overload, horror stories of threading bugs, and MRI's lack of native threading support are probably the reasons, but these are powerful constructs that every developer should understand. We'll tour the basic synchronization primitives that Ruby provides, and I'll show you where each is appropriate, covering potential pitfalls and hazards along the way. We'll see the strengths and limitations of the way MRI, Rubinius, and JRuby run our multithreaded code, and the implications that that has on our code's progress.

Published in: Technology, Education

Threading and Concurrency in Ruby

  1. 1. Ruby Threads
  2. 2. Ruby Threads
  3. 3. @tim_raymond reviewed.com
  4. 4. “So… why do I want to work with Ruby’s threads?”
  5. 5. “HN says…”
  6. 6. Synchronization issues crop up everywhere
  7. 7. Mutexes Easiest way to synchronize things in Ruby Create a Mutex Call Mutex.synchronize, and at most one thread will be able to run the content of the block
  8. 8. Condition Variable Allows us to wait for certain conditions to become true before continuing, rather than burning CPU doing nothing Allows threads to immediately give up their lock so another thread can make that condition true
  9. 9. Puma::ThreadPool
  10. 10. Puma::ThreadPool
  11. 11. Puma::ThreadPool
  12. 12. Puma::ThreadPool
  13. 13. Puma::ThreadPool
  14. 14. Monitors Think Mutex + Condition Variable Provides a better structure Allows for recursive locking Threads enter a waiting queue and are signaled by threads that currently have the monitor
  15. 15. Semaphores Basically variables handled by the runtime/OS Earliest formulation of synchronization Two types: counting and binary Two basic operations: P & V Ruby doesn’t provide them… for good reason
  16. 16. Software Transactional Memory No first class support, gems have become stale Allows writers to take an optimistic view of the world Onus is on readers to clean up the writers' mess
  17. 17. http://bit.ly/1eMxQON http://ibm.co/18Yzq1T
  18. 18. Celluloid Actors for Ruby
  19. 19. MRI's Threads One pthread for every Thread.new …but every thread has to obtain a VM-wide Mutex, which is the GIL or Global Interpreter Lock Better for downloading Tweets while writing ones a user cares about on a Socket, than for mining your Cryptocurrency of choice
  20. 20. Rubinius & jRuby’s Threads One thread per Thread.new Rubinius and jRuby have no Global Interpreter Lock, so they’re generally going to be better if your work is CPU bound
  21. 21. The Safest Path to Concurrency Don't do it If you must do it, don't share data across threads If you must share data across threads, don't share mutable data If you must share mutable data across threads, synchronize access to that data Source: Working with Ruby Threads - Jesse Stormier
  22. 22. Thanks! @tim_raymond

×