Look out! Gotchas of Using Threads in Ruby
summary
Ruby MRI threads are concurrent but not parallel so CPU work doesn’t speed up while JRuby/truffle and ractors can run in parallel, and naive shared mutable state produces real errors like 11 extra elements in a counter example. Practical fixes: use thread coordination with Queue or join, protect initialization with a shared Mutex around the assignment, prefer Concurrent::Map or Atomic classes from concurrent-ruby, freeze or copy shared data, and avoid sleeps in tests—use waiter loops or rspec-wait.
AI-written summary · about
chapters (14)
- 0:00Thread gotchas in Ruby
- 2:18Why concurrency matters
- 4:22Concurrency models in Ruby
- 9:40Threads concurrent not parallel
- 12:13Parallelism in JRuby TruffleRuby
- 13:38Avoid sleeps in tests
- 18:31Hash iteration risks
- 23:27Atomic behavior explained
- 29:52Heisenbugs in Ruby code
- 30:36Non-atomic initialization
- 34:09Sharing mutable state
- 35:35Lazy loading in threads
- 36:38Thread safety recap
- 38:26Q&A
related talks
47mWhat All Rubyists Should Know About Threads
28mConcurrency for Rubyists
33mConcurrency for !Dummies (Who Don't Get It (...Yet))
23mParallel Processing With Ruby
33mDear God What Am I Doing? Concurrency and Parallel Processing
19mThread Safety First!
38m"Parallel and Thread-Safe Ruby at High-Speed with TruffleRuby"
38mScaling Ruby with Actors, or How I Learned to Stop Worrying and Love Threads
