class Concurrent::ThreadSafe::Util::Adder
@!visibility private
consumption.
this class is significantly higher, at the expense of higher space
characteristics. But under high contention, expected throughput of
control. Under low update contention, the two classes have similar
as collecting statistics, not for fine-grained synchronization
multiple threads update a common sum that is used for purposes such
This class is usually preferable to single Atomic
reference when
variables maintaining the sum.
Method sum
returns the current total combined across the
the set of variables may grow dynamically to reduce contention.
sum. When updates (method add
) are contended across threads,
One or more variables that together maintain an initially zero
Original source code available here:
available in public domain.
A Ruby port of the Doug Lea’s jsr166e.LondAdder class version 1.8
def add(x)
def add(x) if (current_cells = cells) || !cas_base_computed {|current_base| current_base + x} was_uncontended = true hash = hash_code unless current_cells && (cell = current_cells.volatile_get_by_hash(hash)) && (was_uncontended = cell.cas_computed {|current_value| current_value + x}) retry_update(x, hash, was_uncontended) {|current_value| current_value + x} end end end
def decrement
def decrement add(-1) end
def increment
def increment add(1) end
def reset
def reset internal_reset(0) end
def sum
occur while the sum is being calculated might not be
updates returns an accurate result, but concurrent updates that
atomic snapshot: Invocation in the absence of concurrent
Returns the current sum. The returned value is _NOT_ an
def sum x = base if current_cells = cells current_cells.each do |cell| x += cell.value if cell end end x end