Assignments and thread safety

Just answering to this point: there's a trick called the double-checked
locking pattern that avoids the unnecessary lock. I'm not sure if it helps
any in your situation:

@@mutex.synchronize {@a ||= unless @a} unless @a

  @@mutex.synchronize { @a ||= } unless @a
  @a << foo

Thanks!

If you see my solution (which is already in your mail as a quote, just
in the line after the one proposed by you), it's almost the same, just
there is no "unless" in the scope of the mutex. So I kinda figured out
that this is the way to go... Is there any use of that inner "unless"?
I'd guess no...

Er, no, I was just being stupid :slight_smile: Since ||= already checks if @a exists,
the pattern is unnecessary in most cases; however, being ||= may not be
atomic, perhaps to better convey my idea I should have written:

@@mutex.synchronize {@a = unless @a} unless @a

Regards,
Csaba

E

···

On Mon, February 28, 2005 8:14 am, Csaba Henk said:

On 2005-02-27, ES <ruby-ml@magical-cat.org> wrote:

On Sun, February 27, 2005 10:44 am, Csaba Henk said: