31#ifndef __FASTJET_THREAD_SAFETY_HELPERS_HH__
32#define __FASTJET_THREAD_SAFETY_HELPERS_HH__
43#include "fastjet/internal/base.hh"
44#include "fastjet/config.h"
47#ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
52FASTJET_BEGIN_NAMESPACE
54namespace thread_safety_helpers{
67 AtomicCounter() : _count{0}{}
70 AtomicCounter(
const T &count) : _count{count}{}
74 AtomicCounter(
const AtomicCounter &other) : _count{other._count.load()}{}
78 operator T()
const{
return _count.load();}
81 T get()
const{
return _count.load();}
84 void set(
const T new_value){
85 _count.store(new_value);
109 while (_count < std::numeric_limits<T>::max()
110 && !(_count.compare_exchange_strong(count, count+1)));
116 inline T operator++(){
122 inline T operator++(
int){
127 std::atomic<T> _count;
138 FirstTimeTrue(): _first_time{true}{}
140 FirstTimeTrue(
const FirstTimeTrue &other) : _first_time{other._first_time.load()}{}
149 bool expected =
true;
157 return _first_time.compare_exchange_strong(expected,
false,
158 std::memory_order_seq_cst,
159 std::memory_order_relaxed);
162 std::atomic<bool> _first_time;
171FASTJET_BEGIN_NAMESPACE
173namespace thread_safety_helpers{
194 operator T()
const{
return _count;}
197 T
get()
const{
return _count;}
200 void set(
const T new_value){
206 unsigned int count = _count;
207 if (_count < std::numeric_limits<T>::max()){ _count++; }
235 if (!_first_time) {
return false;}
(would) provides a thread-safe counter (with CXX11 features)
AtomicCounter()
default ctor
AtomicCounter(const AtomicCounter &other)
copy ctor
void set(const T new_value)
set the counter to a given value
T step()
step the counter and return the value just before it was stepped
T operator++()
override the ++ operator prefix version
T operator++(int)
override the ++ operator postfix version
AtomicCounter(const T &count)
ctor with initialisation
T get() const
get the count
provides an object wich will return "true" the first time () is called and false afterwards