Concurrency: Rubies, plural

Elise Huard

Moore’s Law finally hit in 2002. Multiprocessor response, plus non-uniform memory access. Important to be aware of concurrency.

But … forget all that (mostly)

Concurrent programming != parallel computing

Thread level paralleism vs instruction level

Not orderly more like a stream of cars on a busy road that soldiers in lock step.


  • Preemtive → told to yield
  • Cooperative → thread yields control

Threads are the unit of execution. Process container/memory space of one or more threads. Context of thread.



  • 1.8 green
  • 1.9 Native threads MRI 1.9, Rubinius


Global Interpreter Lock

Only one thread executed at a time, scheduling of threads fair. Main and time. Blocking region to allow limited concurrency. (Blocking IO driver)

Other Rubies

Parallel quicksort

Easy to parallelise, put each tree in its own thread.


Separate state
disadvantege overhead to cpawinign & context switching

Ruby: for and IPC: IO.pipe Mmap


Fibers – not parallel. Cooperative scheduling, coroutines – passing between fibers at same point.


Rubinius – VM per native thread. Looks out of date now and not supported?

Shared state will melt your brain

  • non-determinism
  • atomicity
  • deadlock
  • livelock
  • fairness/starvation
  • race conditions

Actor model

named actors: no shared state – asynchronous message passing fire & forget


Communication Sequential Processing

  • Process calculi
  • Events, processes
  • Synchronous message passing (rendez-vous)
  • named channels – dual to Actor model


  • Erlang
  • Clojure
  • Go (CSP)
  • Haskell (several)
  • Scala (Actors)


  • Functional programming – no side effects, immutable data
  • Nothing shared
  • message passing


  • Actor model: Actors, asynch message passing
  • actors = “green processes:
  • efficient VM (SMP since R12B)
  • high reliability


  • Actions in the language, threds with inbox
  • VM actors (not working at the moment)

Ruby: Revactor

  • elrang-like senatics: actor spawn/receive, filter
  • Fibers (so coop scheduling)
  • Revactor::TCP for non-blocking


  • Fairly low-level fit for systems programming
  • static typing
  • goroutines: parallel execution – sort of async lightweight thread
  • channels (sync named)


Functional, lisp-like

Concurrency, Sottware Transactional Memory system

  • Vars = variable state is thread isolated
  • Refs = shared, and mitation within a transaction (atominc. consistent, isolated) – Multiversion concurrency control

Ruby: STM

  • @mentalguy

Kernel stuff

Some of these problems have been solved before.


Dirkjan Bussink, Nedap

1.1 is out! Bug fixes, block inlining, new debugger API and included debugger as a reference, improved GIL (used ideas from Python 3.2)

2006 Ruby interpreter in Ruby, is it possible to write more of Ruby in Ruby?

2010 1.0 release

2 virtual machines (3 if you count the 1st VM in Ruby) new VM in C++ better fits Ruby’s model from C++

1.75 garbage collectors

3 bytecode compilers (based on MRI parser)

1 Ruby core library

2 primitives systems – call into VM to do work for you

JIT compilers


Create a reference for Rubinius and create in Ruby.


Compacting garbage garbage collector
Generational – long lived objects out the way not scan every object every time

Fast workers die young

Instance variable packing, keeps track of instance variables used in compilation


InlineCache – simple technique, remember where method was found. But need to invalidate caches if class has been changed. Method calling much faster than MRI. Counts how many times you call the method, and looks at chain of calls. JIT using LLVM – intermediate representation (IR) assembly language for LLVM.

Debugging – shows backtrace, disassemble into bytecode

The awesome backtrace, proper alignment, different colours for what kind of code


Measuring is knowing and guessing is often wrong. Runs twice, warm up to get inlining and optimisations. Profiler graphs too so that you can trace why things take time when they don’t in isolation.


One patch accepted == commit access

Rather have a policy of being very open.

rbx my_awesome_code.rb


  • 1.9 support new Hash syntax, encodings
  • Windows support
  • Hydra GIL removed

Audio Synthesizers

Jeff Rose, Sam X

Clojure. Serge modular synth

Modules/patch cables/no save button

Software synths like Circle – can’t get into the guts and rearrange things. Visial programming analogues. Finally found SuperCollider – own language based on Smalltalk. Getting into clojure, connect it to back end audio engine of SuperCollider …

Generate music / hack. Representing a waveform as arrays of numbers and manipulating them. Software – sound card DAC takes sample values and makes smooth waveform. Voltate corresponds to position of an electromagnet.

Crash course in Clojure …

Overtone – library around SuperCollider API

Compile synthesiser in terms of DSP units and function you can call to trigger it.

Live coding session followed. It was ok for a while.