Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.

Class template thread_safe

boost::histogram::accumulators::thread_safe — Thread-safe adaptor for builtin integral and floating point numbers.


// In header: <boost/histogram/accumulators/thread_safe.hpp>

template<typename T> 
class thread_safe : public std::atomic< T > {
  // types
  typedef std::atomic< T > super_t;

  // construct/copy/destruct
  thread_safe() noexcept;
  thread_safe(const thread_safe &) noexcept;
  thread_safe & operator=(const thread_safe &) noexcept;
  thread_safe & operator=(T);

  // public member functions
  void operator+=(T);
  void operator++();


This adaptor uses std::atomic to make concurrent increments and additions safe for the stored value.

On common computing platforms, the adapted integer has the same size and alignment as underlying type. The atomicity is implemented with a special CPU instruction. On exotic platforms the size of the adapted number may be larger and/or the type may have different alignment, which means it cannot be tightly packed into arrays.

Template Parameters

  1. typename T

    type to adapt, must be supported by std::atomic.

thread_safe public construct/copy/destruct

  1. thread_safe() noexcept;
  2. thread_safe(const thread_safe & o) noexcept;
  3. thread_safe(T arg);
  4. thread_safe & operator=(const thread_safe & o) noexcept;
  5. thread_safe & operator=(T arg);

thread_safe public member functions

  1. void operator+=(T arg);
  2. void operator++();