A reference counter with atomic increment/decrement. More...
#include "utility.hpp"
Public Types | |
using | type = long |
Public Member Functions | |
ref_count (const ref_count &)=delete | |
ref_count & | operator= (const ref_count &)=delete |
ref_count (ref_count &&)=delete | |
ref_count & | operator= (ref_count &&)=delete |
type | get_value () const noexcept |
void | set_value (type _count) noexcept |
type | inc () |
type | dec () noexcept |
A reference counter with atomic increment/decrement.
The default constructor creates a new ref_count
object with the count value = 1.
Atomic operations on the ref_count
object provide the following memory ordering semantics:
Member function | Memory ordering |
---|---|
get_value() | acquire |
set_value() | release |
inc() | relaxed |
dec() | release |
Thus the client code can use get_value()
function to check the current number of the variables referencing a counted object and be sure to be synchronized-with the last dec()
operation (i.e. to see all the results of non-atomic memory changes happened-before the last dec()
operation which should normally occurs when one of the variable of the counted object is destroyed on going out of its scope). Since only inc()
/dec()
operations are supposed to be normally used for changing the count value, it is considered to be an unusual case where using of set_value()
function can be necessary.
inc()
throws std::runtime_error
if the current value to be incremented is 0 as this circumstance is considered as a variable of the counted object is being constructed/copied from a reference just becoming a dangling one.
Definition at line 229 of file utility.hpp.