Class: Concurrent::AtomicFixnum
Relationships & Source Files | |
Inherits: |
Concurrent::AtomicFixnumImplementation
|
Defined in: | lib/concurrent-ruby/concurrent/atomic/atomic_fixnum.rb |
Overview
A numeric value that can be updated atomically. Reads and writes to an atomic fixnum and thread-safe and guaranteed to succeed. Reads and writes may block briefly but no explicit locking is required.
## 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.
Performance:
“‘ Testing with ruby 2.1.2 Testing with MutexAtomicFixnum
…
3.130000 0.000000 3.130000 ( 3.136505)
Testing with CAtomicFixnum
…
0.790000 0.000000 0.790000 ( 0.785550)
Testing with jruby 1.9.3 Testing with MutexAtomicFixnum
…
5.460000 2.460000 7.920000 ( 3.715000)
Testing with Concurrent::JavaAtomicFixnum
…
4.520000 0.030000 4.550000 ( 1.187000)
“‘
Class Method Summary
-
.new(initial = 0)
constructor
Creates a new
AtomicFixnum
with the given initial value.
Instance Method Summary
-
#compare_and_set(expect, update) ⇒ Boolean
Atomically sets the value to the given updated value if the current value == the expected value.
-
#decrement(delta = 1) ⇒ Fixnum
Decreases the current value by the given amount (defaults to 1).
-
#increment(delta = 1) ⇒ Fixnum
Increases the current value by the given amount (defaults to 1).
-
#inspect
Alias for #to_s.
- #to_s ⇒ String (also: #inspect)
-
#update {|Object| ... } ⇒ Object
Pass the current value to the given block, replacing it with the block’s result.
-
#value ⇒ Fixnum
Retrieves the current
Fixnum
value. -
#value=(value) ⇒ Fixnum
Explicitly sets the value.
Constructor Details
.new(initial = 0)
Creates a new AtomicFixnum
with the given initial value.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_fixnum.rb', line 136
class AtomicFixnum < AtomicFixnumImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], value end alias_method :inspect, :to_s end
Instance Method Details
#compare_and_set(expect, update) ⇒ Boolean
Atomically sets the value to the given updated value if the current value == the expected value.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_fixnum.rb', line 136
class AtomicFixnum < AtomicFixnumImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], value end alias_method :inspect, :to_s end
#decrement(delta = 1) ⇒ Fixnum
Decreases the current value by the given amount (defaults to 1).
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_fixnum.rb', line 136
class AtomicFixnum < AtomicFixnumImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], value end alias_method :inspect, :to_s end
#increment(delta = 1) ⇒ Fixnum
Increases the current value by the given amount (defaults to 1).
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_fixnum.rb', line 136
class AtomicFixnum < AtomicFixnumImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], value end alias_method :inspect, :to_s end
#inspect
Alias for #to_s.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_fixnum.rb', line 142
alias_method :inspect, :to_s
#to_s ⇒ String
Also known as: #inspect
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_fixnum.rb', line 138
def to_s format '%s value:%s>', super[0..-2], value 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_fixnum.rb', line 136
class AtomicFixnum < AtomicFixnumImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], value end alias_method :inspect, :to_s end
#value ⇒ Fixnum
Retrieves the current Fixnum
value.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_fixnum.rb', line 136
class AtomicFixnum < AtomicFixnumImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], value end alias_method :inspect, :to_s end
#value=(value) ⇒ Fixnum
Explicitly sets the value.
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_fixnum.rb', line 136
class AtomicFixnum < AtomicFixnumImplementation # @return [String] Short string representation. def to_s format '%s value:%s>', super[0..-2], value end alias_method :inspect, :to_s end