ruby memory model
DESCRIPTION
TRANSCRIPT
Ruby Memory ModelHari Krishnan
@harikrishnan83
Why this talk?
Memory model is not about
A quick exercise
Increment operation is not a single instruction
● Retrieve the current value of @count.● Increment the retrieved value by 1.● Store the incremented value back in
@count.
Thread A Thread B
@count = 0Load @count = 0
Increment
Store
Load
Increment
Store
@count = 1
@count = 2
@count = 1
Thread A Thread B
@count = 0Load @count = 0
Increment
Store
Load
Increment
Store@count = 1
Where does reordering happen
Core 1
Cache
Core 2
Cache
Compiler
Memory
What is a memory model?A memory model describes the interactions of threads through memory
and their shared use of the data
Atomicity
Visibility
Ordering
Should rubyists care about this?
Ruby is single threaded
Are single threaded languages like Ruby
thread safe?
Does single core imply thread safe?
Concurrency is about InterleavingsThread A - Load
Thread A - Increment
Thread B - Load
Thread A - Store
Thread B - Increment
Thread B - Store
Threading in Ruby
How is Ruby made Single Threaded
● 1.8 - Green Threads - Global Interpreter Lock
● 1.9 - OS Threads - Global VM Lock
Does GIL make you thread safe?
How does GIL work?
Ruby Thread 1 Ruby Thread 2
OS Thread 1 OS 2
Timer ThreadWake up!Interrupt
GIL Details
● GIL is acquired at start of Thread block and released once done
● To ensure fairness a timer thread sends an interrupt to the Thread holding GIL when other threads are waiting
● The Thread holding GIL may choose to release it based on many parameters
Refer to this excellent blog post by Jesse Storimer - http://www.jstorimer.com/blogs/workingwithcode/8085491-nobody-understands-the-gil
Now do you still think it is thread safe?
Never depend on GIL for Thread Safety
● GIL does not have a specification● It also ties us to MRI● Rubinius and JRuby do not have GIL
Ruby does not have a documented Memory
Model
Ruby right now depends on underlying Virtual Machines
● MRI is not much of an interpreter. It is a VM. - No specification for Memory Model
● JRuby - JVM - JSR 133● Rubinius - LLVM for JIT -
What do we do?
Start Engineering Code for Thread Safety
Do not leave innocent accessors lying around
Methods that mutate parameters are generally
dangerous
Gems to the rescue
● atomic - https://github.com/headius/ruby-atomic
● thread_safe - https://github.com/headius/thread_safe
Thank you!Hari Krishnan
@harikrishnan83