Concurrent Programming and Threads in Ruby - a reading list
Many rubyists consider threads in Ruby as somewhat of an arcane knowledge, though in reality they’re a very well researched and understood concept. Of course, writing effective multithreaded concurrent programs requires certain amount of knowledge and discipline from the programmer, but there’s nothing that a smart one can’t learn if he wants to.
To help with the task, awesome @brainopia compiled a list of recommended reading on the topic of concurrency and threads. All kudos go to @brainopia, and the original list in Russian as available as a gist here:
Following is the translation of that list in English, for all my non-Russian-speaking friends.
First things first – the basic concepts and terminology. Don’t be afraid of the book on an unknown OS – it describes the basics extremely well, and the underlying concepts are the same in every OS and every language.
You should read it at least until “Starting a process” part, and if you’re not afraid of C and want to know more about low-level workings of threads, POSIX system calls etc – keep reading until the end.
When you’re done with this part, you should understand what is a process, thread, mutex, priorites, semaphores, scheduler, contex-switch, kernel states.
2. Ruby – Threads and Fibers
Now, let’s dig into Ruby-specific things.
Overview of threads API in Ruby: TutorialsPoint – Ruby Multithreading
Basic summary of Green Threads (Ruby 1.8) and GIL (Global Interpreter Lock, Ruby 1.9): Leonardo Borges – Understanding Ruby Threads
More about GIL: a.blog.about.code – The Ruby Global Interpreter Lock
With the new knowledge handy, let’s look into the different implementations of threads in various popular (and less so) versions of Ruby: Engine Yard Developer Blog – Ruby, Concurrency and You
Adding fibers and CPU/IO-bound threads into the pot: Engine Yard Developer Blog – Concurrency, Real and Imagined, in MRI; Threads
And more on fibers: Paul Barry – Fibers in Ruby 1.9
A practical example of using fibers in Ruby: Dave Thomas – Pipelines Using Fibers in Ruby 1.9
And how one can emulate fibers with threads, for Ruby 1.8: https://gist.github.com/tmm1/4631
3. Event Loops
Event loops are all the rage these days, so you can’t miss this topic.
Essential knowledge on event loops: Engine Yard Developer Blog – What Are Events, Why Might You Care, and How Can EventMachine Help? Practicing Ruby – Event loop demystifed
Untangling event callbacks, and writing maintanable evented code: Ilya Grigorik – Untangling Evented Code with Ruby Fibers Dotan Nahum – Pragmatic Concurrency With Ruby
And to finish it off, some great talks on the topic of synchronisation and concurrency: Mike Perham @ RubyConf 2011 – Scaling Ruby with Actors, or How I Learned to Stop Worrying and Love Threads Jose Valim @ Rocky Mountain Ruby 2012
And on Celluloid (concurrent programming framework) and Sidekiq (efficient messaging): Tony Arcieri @ RubyConf 2012 – The Celluloid Ecosystem Mike Perham @ RubyConf 2012 – Asynchronous Processing for Fun and Profit
And that’s all – now go and write something awesome!