Class: Concurrent::TVar

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent/tvar.rb

Overview

A ‘TVar` is a transactional variable - a single-element container that is used as part of a transaction - see `Concurrent::atomically`.

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ TVar

Create a new ‘TVar` with an initial value.



12
13
14
15
16
# File 'lib/concurrent/tvar.rb', line 12

def initialize(value)
  @value = value
  @version = 0
  @lock = Mutex.new
end

Instance Method Details

#valueObject

Get the value of a ‘TVar`.



19
20
21
22
23
# File 'lib/concurrent/tvar.rb', line 19

def value
  Concurrent::atomically do
    Transaction::current.read(self)
  end
end

#value=(value) ⇒ Object

Set the value of a ‘TVar`.



26
27
28
29
30
# File 'lib/concurrent/tvar.rb', line 26

def value=(value)
  Concurrent::atomically do
    Transaction::current.write(self, value)
  end
end