Project

General

Profile

Actions

Feature #12021

open

Final instance variables

Added by pitr.ch (Petr Chalupa) almost 9 years ago. Updated almost 3 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:73440]

Description

Having a final instance variables in Ruby will allow: to construct thread-save immutable objects without additional synchronisation on instance variable reading

# Immutable and thread-safe 
class TreeNode
  attr :left, :right, :value, final: true
  attr_reader :left, :right, :value

  def initialize(left, right, value)
    @left, @right, @value = left, right, value
  end
end

And to fix the an issue shown in the following example:

attr :lock, final: true
def initialize
  @lock = Mutex.new
  # ...
end  
   
def a_protected_method
  @lock.synchronize do
    # ...
  end
end

The issue lies in initialization of instance variable @lock which is not ensured to be visible in subsequent call to a_protected_method method on a different thread.

Summary can be found in this document https://docs.google.com/document/d/1c07qfDArx0bhK9sMr24elaIUdOGudiqBhTIRALEbrYY/edit#.

The aggregating issue of this effort can be found here.


Related issues 2 (2 open0 closed)

Related to Ruby master - Feature #12019: Better low-level support for writing concurrent librariesAssignedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #12334: Final/Readonly Support for Fields / Instance VariablesOpenActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0