Ruby core classes aren't thread safe
jstorimer.com
Ruby core classes aren't thread safe
1–10 of 47 posts
Re: Ruby core classes aren't thread safe
#2Ruby threads seems to be a fairly narrow topic on which to base a book; I'll look forward to seeing what all the book covers.
Re: Ruby core classes aren't thread safe
#3Re: Ruby core classes aren't thread safe
#4Re: Ruby core classes aren't thread safe
#5Re: Ruby core classes aren't thread safe
#6Does ruby have a spec? If not, I don't really see this as a problem... synchronization, especially in a dynamic language that's already really slow, would just add more overhead.
Re: Ruby core classes aren't thread safe
#7* Loading an instance-local collection
* Fetching a numeric value from the collection
* Incrementing or decrementing the numeric value
* Putting the incremented value back
Even if the collection is "synchronized" (each method call takes a collection-local lock), because the value is altered outside the collection there's no way for the change to be atomic unless it's wrapped in a transaction block or protected by a lock. As far as I can think, the only ways for core classes to "be thread-safe" considering the example (keeping mutable collections semantics) would be to either have collections dedicated specifically to the operation such as Java6's AtomicIntegerArray (http://docs.oracle.com/javase/6/docs/api/java/util/concurren...), or to have the collection apply the operation internally e.g. Hash#apply(key, &operation) used roughly like this:
def decrease item
@stock.apply(item) {|from| from - 1}
endRe: Ruby core classes aren't thread safe
#8This still doesn't explain why the MRI implementation is accidentally threadsafe. Why doesn't the interpreter switch threads after reading the value from the hash but before storing the updated value?
Re: Ruby core classes aren't thread safe
#9This still doesn't explain why the MRI implementation is accidentally threadsafe. Why doesn't the interpreter switch threads after reading the value from the hash but before storing the updated value?
Re: Ruby core classes aren't thread safe
#10This still doesn't explain why the MRI implementation is accidentally threadsafe. Why doesn't the interpreter switch threads after reading the value from the hash but before storing the updated value?
So I'd expect that even on MRI the original code could in fact fail to yield 0 once in a while. That's what'd happen on CPython