"is ||= in ruby thread safe?" Code Answer


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.

By Babken Vardanyan on May 20 2022

Answers related to “is ||= in ruby thread safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.