Hi there?

Is "$a ||= 1" thread safe? I'd guess so, as "||=" is one piece of
operator. (And one operator/function call can't be interruted by the
scheduler, I'd guess so... is that right?)

And what's about "$a or $a = 1" ? Now I don't see any guarantee that if
such a code runs in multiple threads, then assignment occurs only once.

However, I couldn't produce such a case. The code

ruby -e '20.times { |i| Thread.new {
     sleep 1
     $foo ? p($foo) : $foo = i
  }
}; (Thread.list - [Thread.current]).each{|t| t.join}'

always accurately prints 19 lines (and similary for other numbers: if I
use some n instead of 20, I get n-1 lines).

Is it just an accident, or we can know somehow that this code is thread
safe? Where do the limits of thread safety lie?

Csaba