Your SlideShare is downloading. ×
Ruby Threads
Ruby Threads
@tim_raymond
reviewed.com
“So… why do I want to work with
Ruby’s threads?”
“HN says…”
Synchronization issues crop up
everywhere
Mutexes

Easiest way to synchronize things in Ruby
Create a Mutex
Call Mutex.synchronize, and at most one thread will be
a...
Condition Variable

Allows us to wait for certain conditions to become true
before continuing, rather than burning CPU doi...
Puma::ThreadPool
Puma::ThreadPool
Puma::ThreadPool
Monitors

Think Mutex + Condition Variable
Provides a better structure
Allows for recursive locking
Threads enter a waitin...
Software Transactional
Memory

No first class support, gems have become stale
Allows writers to take an optimistic view of ...
http://bit.ly/1eMxQON
http://ibm.co/18Yzq1T
Celluloid
Actors for Ruby
MRI's Threads

One pthread for every Thread.new
…but every thread has to obtain a VM-wide Mutex,
which is the GIL or Globa...
Rubinius & jRuby’s Threads

One thread per Thread.new
Rubinius and jRuby have no Global Interpreter Lock, so
they’re gener...
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 acros...
Thanks!
@tim_raymond
Threading and Concurrency in Ruby
Threading and Concurrency in Ruby
Threading and Concurrency in Ruby
Threading and Concurrency in Ruby
Threading and Concurrency in Ruby
Threading and Concurrency in Ruby
Threading and Concurrency in Ruby
Threading and Concurrency in Ruby
Threading and Concurrency in Ruby
Threading and Concurrency in Ruby
Threading and Concurrency in Ruby
Threading and Concurrency in Ruby
Upcoming SlideShare
Loading in...5
×

Threading and Concurrency in Ruby

661

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
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
661
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Transcript of "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

×