1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
//
// AtomicCounter.h
//
// Library: Foundation
// Package: Core
// Module: AtomicCounter
//
// Definition of the AtomicCounter class.
//
// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Foundation_AtomicCounter_INCLUDED
#define Foundation_AtomicCounter_INCLUDED
#include "Poco/Foundation.h"
#include <atomic>
namespace Poco {
class Foundation_API AtomicCounter
/// 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 functionality.
{
public:
typedef int ValueType; /// The underlying integer type.
AtomicCounter();
/// Creates a new AtomicCounter and initializes it to zero.
explicit 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.
~AtomicCounter();
/// Destroys the AtomicCounter.
AtomicCounter& operator = (const AtomicCounter& counter);
/// Assigns the value of another AtomicCounter.
AtomicCounter& operator = (ValueType value);
/// Assigns a value to the counter.
operator ValueType () const;
/// Converts the AtomicCounter to ValueType.
ValueType value() const;
/// Returns the value of the counter.
ValueType operator ++ (); // prefix
/// Increments the counter and returns the result.
ValueType operator ++ (int); // postfix
/// Increments the counter and returns the previous value.
ValueType operator -- (); // prefix
/// Decrements the counter and returns the result.
ValueType operator -- (int); // postfix
/// Decrements the counter and returns the previous value.
bool operator ! () const;
/// Returns true if the counter is zero, false otherwise.
private:
std::atomic<int> _counter;
};
//
// inlines
//
inline AtomicCounter::operator AtomicCounter::ValueType () const
{
return _counter.load();
}
inline AtomicCounter::ValueType AtomicCounter::value() const
{
return _counter.load();
}
inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix
{
return ++_counter;
}
inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix
{
return _counter++;
}
inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix
{
return --_counter;
}
inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix
{
return _counter--;
}
inline bool AtomicCounter::operator ! () const
{
return _counter.load() == 0;
}
} // namespace Poco
#endif // Foundation_AtomicCounter_INCLUDED
|