Thread Safe Enumerator in Ruby

Are YARV Arrays' push/pop methods thread-safe?

Accessing arrays is thread-safe in MRI/YARV (and only there) most of the time due to its global interpreter lock (GIL), thus mostly by accident.

You still would have to make sure that you are only performing a single operation each time and avoid read/write constructs. In other Ruby implementations like Rubinius or JRuby, Arrays are explicitly not thread safe.

With that being said, Ruby ships with a different primitive for inter-thread communication, which co-incidentaly is about the only class in MRI/VARV which is explicitly thread-safe: the Queue. It supports pushing and poping objects in a thread-safe way.

Take this example from Ruby's documentation:

queue = Queue.new

producer = Thread.new do
5.times do |i|
sleep rand(i) # simulate expense
queue << i
puts "#{i} produced"
end
end

consumer = Thread.new do
5.times do |i|
value = queue.pop
sleep rand(i/2) # simulate expense
puts "consumed #{value}"
end
end

There also exists a well-maintained project called concurrent-ruby which offers a lot of powerful primitives for concurrent programming across threads.

Is ||= in Ruby thread safe?

It depends on the implementation. Be aware that x ||= y expands to x || x = y, and that x = y is only executed if x is either false or nil.

With that said, the C implementation of the Ruby language should be completely thread safe.

YARV uses native threads in order to implement concurrency, which do run in true parallel. However, in order to maintain backward compatibility, a global, interpreter-wide lock was introduced.

JRuby, however, imposes no internal locking on your code, so you must manually synchronize your calls when needed.

See another answer I've given about the subject for more details. Also, read this excellent answer by Jörg W Mittag for a more in-depth look at the threading models of the various Ruby implementations.

Thread-safe external process in ruby, plus checking exitstatus

Although rb_last_status (aka $?) is a global variable, Ruby does a magic trick to make it per-thread: In rb_thread_save_context, it stores rb_last_status in the old thread's structure; rb_thread_restore_context sets rb_last_status from the saved value. If you've got a race condition involving $?, it's a Ruby bug.



Related Topics



Leave a reply



Submit