class Poco::AtomicCounter
Overview
This class implements a simple counter, which provides atomic operations that are safe to use in a multithreaded environment. Moreā¦
#include <AtomicCounter.h> class AtomicCounter { public: // typedefs typedef int ValueType; // construction AtomicCounter(); AtomicCounter(ValueType initialValue); AtomicCounter(const AtomicCounter& counter); // methods AtomicCounter& operator=(const AtomicCounter& counter); AtomicCounter& operator=(ValueType value); operator ValueType() const; ValueType value() const; ValueType operator++(); ValueType operator++(int); ValueType operator--(); ValueType operator--(int); bool operator!() const; };
Detailed Documentation
This class implements a simple counter, which provides atomic operations that are safe to use in a multithreaded environment.
Typical usage of AtomicCounter is for implementing reference counting and similar things.
On some platforms, the implementation of AtomicCounter is based on atomic primitives specific to the platform (such as InterlockedIncrement, etc. on Windows), and thus very efficient. On platforms that do not support atomic primitives, operations are guarded by a FastMutex.
The following platforms currently have atomic primitives:
- Windows - Mac OS X - GCC 4.1+ (Intel platforms only)
Typedefs
typedef int ValueType
The underlying integer type.
Construction
AtomicCounter()
Creates a new AtomicCounter and initializes it to zero.
AtomicCounter(ValueType initialValue)
Creates a new AtomicCounter and initializes it with the given value.
AtomicCounter(const AtomicCounter& counter)
Creates the counter by copying another one.
Methods
AtomicCounter& operator=(const AtomicCounter& counter)
Assigns the value of another AtomicCounter.
AtomicCounter& operator=(ValueType value)
Assigns a value to the counter.
operator ValueType() const
Returns the value of the counter.
ValueType value() const
Returns the value of the counter.
ValueType operator++()
Increments the counter and returns the result.
ValueType operator++(int)
Increments the counter and returns the previous value.
ValueType operator--()
Decrements the counter and returns the result.
ValueType operator--(int)
Decrements the counter and returns the previous value.
bool operator!() const
Returns true if the counter is zero, false otherwise.