Not sure if threads safety even applies to ||=.
Was originally reading about ActiveSupport::Memoizable and wondered about thread safety there.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It depends on the implementation. Be aware that
x ||= yexpands tox || x = y, and thatx = yis only executed ifxis eitherfalseornil.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.