Method: Concurrent::Synchronization::Object.safe_initialization!

Defined in:
lib/concurrent/synchronization/object.rb

.safe_initialization!Object

By calling this method on a class, it and all its children are marked to be constructed safely. Meaning that all writes (ivar initializations) are made visible to all readers of newly constructed object. It ensures same behaviour as Java’s final fields.

Examples:

class AClass < Concurrent::Synchronization::Object
  safe_initialization!

  def initialize
    @AFinalValue = 'value' # published safely, does not have to be synchronized
  end
end


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/concurrent/synchronization/object.rb', line 52

def self.safe_initialization!
  # define only once, and not again in children
  return if safe_initialization?

  def self.new(*args, &block)
    object = super(*args, &block)
  ensure
    object.full_memory_barrier if object
  end

  @safe_initialization = true
end