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
How to Do "Late" String Interpolation in Ruby
Actioncable - Failed to Upgrade to Websocket in Production
Database Cleaner Not Working in Minitest Rails
How to Destroy a Record Without an Id Column in Ruby Activerecord
How to 'Unload' ('Un-Require') a Ruby Library
How to Fix a Slow Implicit Query on Pg_Attribute Table in Rails
How to Convert a Scientific Notation String to Decimal Notation
How to Set Ca-Bundle Path for Openssl in Ruby
How to Split String into Array as Integers
Using Activerecord Find_In_Batches Method for Deleting Large Data
Is It Necessary to Close Stringio in Ruby
Remove Double Quotes from String
Openssl Trouble with Ruby 1.9.3
Is There an Equivalent of Array#Find_Index for the Last Index in Ruby