qi::Atomic provides support for atomic values, similarly to boost::atomic but
with simpler methods.
Global Classes
class qi::Atomic
Functions (class qi::Atomic)
Members (class qi::Atomic)
qi::Atomic comes with some macros to do one-time initializations in a
thread-safe manner.
An atomic variable allows some operations to be atomic in a multithreaded environment.
#include <qi/atomic.hpp>
qi::Atomic<int> var = 0;
void myFunction()
{
if (var.setIfEquals(0, 1))
std::cout << "Set to one!";
}
int main()
{
boost::thread(myFunction);
boost::thread(myFunction);
boost::thread(myFunction);
qi::os::msleep(100);
}
This program will always print “Set to one!” once, since only one setIfEquals
can succeed. All methods in qi::Atomic are atomic and thus threadsafe, they
all provide a total ordering of operations.
Two macros are defined in this file:
Atomic()Atomic(T value)Atomic(const Atomic& other)operator++()operator--()operator++(int)operator--(int)operator=(T value)operator=(const Atomic<T>& value)setIfEquals(T testValue, T setValue)swap(T value)operator*() constload() constAtomic operations on integrals.
This class allows to do operations on an integral value from multiple threads, with the guarantee that each operation will not lead to a data race.
This is a simplification layer over the standard atomic type. If you understand the standard atomic, it might be preferable to use it.
qi::Atomic<T>::Atomic()¶qi::Atomic<T>::Atomic(T value)¶Brief:
| Parameters: |
|
|---|
Atomic constructor setting value to its parameter.
qi::Atomic<T>::Atomic(const Atomic& other)¶qi::Atomic<T>::operator++()¶Atomic pre-increment of the value.
qi::Atomic<T>::operator--()¶Atomic pre-decrement of the value.
qi::Atomic<T>::operator++(int)¶Atomic post-increment of the value.
qi::Atomic<T>::operator--(int)¶Atomic post-decrement of the value.
qi::Atomic<T>::operator=(T value)¶qi::Atomic<T>::operator=(const Atomic<T>& value)¶qi::Atomic<T>::setIfEquals(T testValue, T setValue)¶Brief:
| Returns: | true if swap was performed |
|---|
If value is testValue, replace it with setValue.
qi::Atomic<T>::swap(T value)¶Brief:
| Returns: | the previously held value |
|---|
Swap the atomic value with value.
qi::Atomic<T>::operator*() const¶Return the contained valu Deprecated since 2.5.0
qi::Atomic<T>::load() const¶