Threading and Concurrency in Ruby

31
Ruby Threads

Transcript of Threading and Concurrency in Ruby

Ruby Threads

Ruby Threads

@tim_raymondreviewed.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 able to run the content of the block

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

Puma::ThreadPool

Puma::ThreadPool

Puma::ThreadPool

Puma::ThreadPool

Puma::ThreadPool

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

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

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

http://ibm.co/18Yzq1T

http://bit.ly/1eMxQON

CelluloidActors 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 Global Interpreter Lock

Better for downloading Tweets while writing ones a user cares about on a Socket, than for mining your Cryptocurrency of choice

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

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

Thanks!@tim_raymond