From: Charles Oliver Nutter Date: 2008-12-24T07:34:36+09:00 Subject: Re: Are all Ruby built-in objects thread safe? J旦rg W Mittag wrote: > A couple of months ago, this actually became an issue. Originally, the > JRuby developers had implemented Arrays to be not safe. One of the big > selling points of JRuby was and still is the promise of true > concurrency and better scalability. So, naturally, people wanted to > take advantage of this feature and started running their concurrent > programs on JRuby. And those programs crashed left and right, because > they didn't lock their Arrays properly. So, the JRuby team decided to > implement thread-safe data structures on their end, so that code that > didn't crash on MRI could be run unmodified on JRuby. Actually, we made a minimal attempt to ensure that concurrent operations against Array were usually safe across threads but also would raise a Ruby-land "ConcurrencyError" when concurrent changes could not be reconciled. It's a trade-off; adding locks to all the core collections would severely penalize performance for what's generally the rare case of concurrent access. And really there should be a separate set of classes with guaranteed concurrency that people can use if the performance considerations of locking are acceptabe for safe concurrent access. So in short, you're absolutely right; nobody should ever rely on the core collections to be thread-safe, even if they happen to be thread-safe by accident in the C implementations right now. That won't be the case on all implementations, and may not even be the case on future versions of the C impls. The safe answer is to ensure you're watching your own back and properly synchronizing access to shared data structures. - Charlie