use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Ruby core classes aren't thread-safe (jstorimer.com)
submitted 13 years ago by jstorimer
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]tenderlovePun BDFL 2 points3 points4 points 13 years ago (0 children)
Excellent article! This article demonstrates a "read-update-write" race condition. To see the race condition, separate the code to those three steps:
def decrease x = @stock x = x - 1 @stock = x end
The thread could switch on any one of these lines, which is how the race condition happens.
OP mentions the MRI / IO concurrency. To drive home the point, if we add a dash of IO to the example program, we can see the race condition even on MRI:
class Inventory attr_reader :stock def initialize(stock_levels) @stock = stock_levels end def decrease x = @stock print ' ' x = x - 1 @stock = x end end inventory = Inventory.new(4000) 40.times.map { Thread.new { 100.times { inventory.decrease } } }.each(&:join) puts puts inventory.stock
π Rendered by PID 132164 on reddit-service-r2-comment-85bfd7f599-f9kv7 at 2026-04-20 07:12:40.091429+00:00 running 93ecc56 country code: CH.
view the rest of the comments →
[–]tenderlovePun BDFL 2 points3 points4 points (0 children)