From: Robert Klemme Date: 2008-12-27T19:54:52+09:00 Subject: Re: Are all Ruby built-in objects thread safe? On 27.12.2008 09:55, Simon Krahnke wrote: > * Robert Klemme (11:23) schrieb: > >> if hash.contains_key? k >> dat = hash[k] >> else >> dat = create_dat(k) >> hash[k] = dat >> end > >> [...] > >> So in this case you need a lock around the _complete_ block of code. >> (Note, the method level lock would work if using a Hash with a default >> block, but this solves only a small portion of the cases.) > > If Hash had a method get_with_default that took a default value or a > block like initialize you could solve these cases. Please read my note: this was just an example for the class of synchronization problems that cannot be solved within the class. You can invent arbitrary more complex scenarios that would not make any sense to be covered by a standard method in class Hash. My point is totally independent of the functionality of class Hash. This is about sequences of method invocations which do not tolerate any state changes by other threads between method calls. Put it differently: in concurrent applications there are many scenarios where lock granularity "method" is insufficient to guarantee correct code. Instead you need explicit locking for larger portions of _client_ code. Kind regards robert