Class: Concurrent::TVar
- Inherits:
-
Synchronization::Object
- Object
- Synchronization::Object
- Concurrent::TVar
- Defined in:
- lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/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`.
## Thread-safe Variable Classes
Each of the thread-safe variable classes is designed to solve a different problem. In general:
-
*Agent:* Shared, mutable variable providing independent, uncoordinated, asynchronous change of individual values. Best used when the value will undergo frequent, complex updates. Suitable when the result of an update does not need to be known immediately.
-
*Atom:* Shared, mutable variable providing independent, uncoordinated, synchronous change of individual values. Best used when the value will undergo frequent reads but only occasional, though complex, updates. Suitable when the result of an update must be known immediately.
-
*AtomicReference:* A simple object reference that can be updated atomically. Updates are synchronous but fast. Best used when updates a simple set operations. Not suitable when updates are complex. AtomicBoolean and AtomicFixnum are similar but optimized for the given data type.
-
*Exchanger:* Shared, stateless synchronization point. Used when two or more threads need to exchange data. The threads will pair then block on each other until the exchange is complete.
-
*MVar:* Shared synchronization point. Used when one thread must give a value to another, which must take the value. The threads will block on each other until the exchange is complete.
-
*ThreadLocalVar:* Shared, mutable, isolated variable which holds a different value for each thread which has access. Often used as an instance variable in objects which must maintain different state for different threads.
-
*TVar:* Shared, mutable variables which provide coordinated, synchronous, change of many stated. Used when multiple value must change together, in an all-or-nothing transaction.
Instance Method Summary collapse
-
#initialize(value) ⇒ TVar
constructor
Create a new ‘TVar` with an initial value.
-
#value ⇒ Object
Get the value of a ‘TVar`.
-
#value=(value) ⇒ Object
Set the value of a ‘TVar`.
Methods inherited from Synchronization::Object
atomic_attribute?, atomic_attributes, attr_atomic, attr_volatile, ensure_safe_initialization_when_final_fields_are_present, safe_initialization!, safe_initialization?
Constructor Details
#initialize(value) ⇒ TVar
Create a new ‘TVar` with an initial value.
16 17 18 19 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/tvar.rb', line 16 def initialize(value) @value = value @lock = Mutex.new end |
Instance Method Details
#value ⇒ Object
Get the value of a ‘TVar`.
22 23 24 25 26 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/tvar.rb', line 22 def value Concurrent::atomically do Transaction::current.read(self) end end |
#value=(value) ⇒ Object
Set the value of a ‘TVar`.
29 30 31 32 33 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/tvar.rb', line 29 def value=(value) Concurrent::atomically do Transaction::current.write(self, value) end end |