Class: Concurrent::AtomicReference
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: |
Concurrent::AtomicReferenceImplementation
|
Defined in: | lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb |
Overview
An object reference that may be updated atomically. All read and write operations have java volatile semantic.
## 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
andAtomicFixnum
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.
Class Method Summary
- .new(value = nil) constructor
Instance Method Summary
-
#compare_and_set(old_value, new_value) ⇒ Boolean
(also: #cas)
Atomically sets the value to the given updated value if the current value == the expected value.
-
#get ⇒ Object
Gets the current value.
-
#get_and_set(new_value) ⇒ Object
Atomically sets to the given value and returns the old value.
-
#inspect
Alias for #to_s.
-
#set(new_value) ⇒ Object
Sets to the given value.
- #to_s ⇒ String (also: #inspect)
-
#try_update {|Object| ... } ⇒ Object
Pass the current value to the given block, replacing it with the block’s result.
-
#try_update! {|Object| ... } ⇒ Object
Pass the current value to the given block, replacing it with the block’s result.
-
#update {|Object| ... } ⇒ Object
Pass the current value to the given block, replacing it with the block’s result.
Constructor Details
.new(value = nil)
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb', line 126
class AtomicReference < AtomicReferenceImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], get end alias_method :inspect, :to_s end
Instance Method Details
#compare_and_set(old_value, new_value) ⇒ Boolean
Also known as: #cas
Atomically sets the value to the given updated value if the current value == the expected value.
that the actual value was not equal to the expected value.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb', line 126
class AtomicReference < AtomicReferenceImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], get end alias_method :inspect, :to_s end
#get ⇒ Object
Gets the current value.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb', line 126
class AtomicReference < AtomicReferenceImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], get end alias_method :inspect, :to_s end
#get_and_set(new_value) ⇒ Object
Atomically sets to the given value and returns the old value.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb', line 126
class AtomicReference < AtomicReferenceImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], get end alias_method :inspect, :to_s end
#inspect
Alias for #to_s.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb', line 133
alias_method :inspect, :to_s
#set(new_value) ⇒ Object
Sets to the given value.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb', line 126
class AtomicReference < AtomicReferenceImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], get end alias_method :inspect, :to_s end
#to_s ⇒ String
Also known as: #inspect
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb', line 129
def to_s format '%s value:%s>', super[0..-2], get end
#try_update {|Object| ... } ⇒ Object
This method was altered to avoid raising an exception by default. Instead, this method now returns nil
in case of failure. For more info, please see: github.com/ruby-concurrency/concurrent-ruby/pull/336
Pass the current value to the given block, replacing it with the block’s result. Return nil if the update fails.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb', line 126
class AtomicReference < AtomicReferenceImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], get end alias_method :inspect, :to_s end
#try_update! {|Object| ... } ⇒ Object
This behavior mimics the behavior of the original #try_update API. The reason this was changed was to avoid raising exceptions (which are inherently slow) by default. For more info: github.com/ruby-concurrency/concurrent-ruby/pull/336
Pass the current value to the given block, replacing it with the block’s result. Raise an exception if the update fails.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb', line 126
class AtomicReference < AtomicReferenceImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], get end alias_method :inspect, :to_s end
#update {|Object| ... } ⇒ Object
Pass the current value to the given block, replacing it with the block’s result. May retry if the value changes during the block’s execution.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb', line 126
class AtomicReference < AtomicReferenceImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], get end alias_method :inspect, :to_s end