SoftBank Robotics documentation What's new in NAOqi 2.8?

qi::Atomic

qi::Atomic provides support for atomic values, similarly to boost::atomic but with simpler methods.

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

Introduction

More...

#include <qi/atomic.hpp>

Public Members

std::atomic<T> _value

Public Functions

Atomic()
Atomic(T value)
Atomic(const Atomic& other)
T operator++()
T operator--()
T operator++(int)
T operator--(int)
Atomic<T>& operator=(T value)
Atomic<T>& operator=(const Atomic<T>& value)
bool setIfEquals(T testValue, T setValue)
T swap(T value)
T operator*() const
T load() const

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.

Members Documentation

std::atomic<T> qi::Atomic<T>::_value

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