SHARE
TWEET

Untitled

a guest Nov 15th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. template<typename T, T COUNT_MIN = std::numeric_limits<T>::min()
  2.                    , T COUNT_MAX = std::numeric_limits<T>::max() >
  3. T SafeIncrement(std::atomic<T> & counter)
  4. {
  5.    static_assert(MIN < MAX, "range wrong");
  6.  
  7.    T current = counter.load();
  8.    T result  = current == MAX  ? MIN  : current + 1;
  9.    while(!counter.compare_exchange_strong(current, result))
  10.    {
  11.        result = current == MAX ? MIN : current + 1;
  12.    }
  13.    return result;
  14. }
RAW Paste Data
Top