9. 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.
10. Thread A Thread B
@count = 0Load @count = 0
Increment
Store
Load
Increment
Store
@count = 1
@count = 2
@count = 1
11. Thread A Thread B
@count = 0Load @count = 0
Increment
Store
Load
Increment
Store@count = 1
24. How does GIL work?
Ruby Thread 1 Ruby Thread 2
OS Thread 1 OS 2
Timer Thread
Wake up!Interrupt
25. 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
29. 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 -