This module provides a set of functions to do operations towards shared mutable counter variables. The implementation does not utilize any software level locking, which makes it very efficient for concurrent access. The counters are organized into arrays with the following semantics:
Counters are 64 bit signed integers.
Counters wrap around at overflow and underflow operations.
Counters are initialized to zero and can then only be written to by adding or subtracting.
Write operations guarantee atomicity. No intermediate results can be seen from a single write operation.
Two types of counter arrays can be created with options
Indexes into counter arrays are one-based. A counter array of size N contains N counters with index from 1 to N.
Identifies a counter array returned from
Create a new counter array of
Argument
Counters will be sequentially consistent. If write operation A is done sequentially before write operation B, then a concurrent reader may see none of them, only A, or both A and B. It cannot see only B.
This is an optimization to achieve very efficient concurrent
Read operations may see sequentially inconsistent results with regard to concurrent write operations. Even if write operation A is done sequentially before write operation B, a concurrent reader may see any combination of A and B, including only B. A read operation is only guaranteed to see all writes done sequentially before the read. No writes are ever lost, but will eventually all be seen.
The typical use case for
Counters are not tied to the current process and are automatically garbage collected when they are no longer referenced.
Read counter value.
Add
Subtract
Write
Despite its name, the
Return information about a counter array in a map. The map has the following keys (at least):
The number of counters in the array.
Approximate memory consumption for the array in bytes.