qi::Atomic¶
qi::Atomic
provides support for atomic values, similarly to boost::atomic but
with simpler methods.
Summary¶
class qi::Atomic
Functions (class qi::Atomic)
Members (class qi::Atomic)
Global Classes
Detailed Description¶
qi::Atomic
comes with some macros to do one-time initializations in a
thread-safe manner.
Atomic Operations¶
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:
Reference¶
qi::Atomic Class Reference¶
Public Functions¶
-
()Atomic
-
(T value)Atomic
-
(const Atomic& other)Atomic
-
T
()operator++
-
T
()operator--
-
T
(int)operator++
-
T
(int)operator--
-
Atomic<T>&
(T value)operator=
-
Atomic<T>&
(const Atomic<T>& value)operator=
-
bool
(T testValue, T setValue)setIfEquals
-
T
(T value)swap
-
T
() constoperator*
-
T
() constload
Detailed Description¶
Atomic 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.
Function Documentation¶
-
qi::Atomic<T>::
Atomic
()¶
-
qi::Atomic<T>::
Atomic
(T value)¶ Brief:
Parameters: - value – The default value of the atomic.
Atomic constructor setting value to its parameter.
-
qi::Atomic<T>::
Atomic
(const Atomic& other)¶
-
T
qi::Atomic<T>::
operator++
()¶ Atomic pre-increment of the value.
-
T
qi::Atomic<T>::
operator--
()¶ Atomic pre-decrement of the value.
-
T
qi::Atomic<T>::
operator++
(int)¶ Atomic post-increment of the value.
-
T
qi::Atomic<T>::
operator--
(int)¶ Atomic post-decrement of the value.
-
Atomic<T>&
qi::Atomic<T>::
operator=
(T value)¶
-
Atomic<T>&
qi::Atomic<T>::
operator=
(const Atomic<T>& value)¶
-
bool
qi::Atomic<T>::
setIfEquals
(T testValue, T setValue)¶ Brief:
Returns: true if swap was performed If value is testValue, replace it with setValue.
-
T
qi::Atomic<T>::
swap
(T value)¶ Brief:
Returns: the previously held value Swap the atomic value with value.
-
T
qi::Atomic<T>::
operator*
()const
¶ Return the contained valu Deprecated since 2.5.0
-
T
qi::Atomic<T>::
load
()const
¶