How to Write a CAS Loop Using std::atomics -- Herb Sutter
Just posted on Sutter's Mill, "Reader Q&A: How to write a CAS loop using std::atomics." From the post:
A colleague asked:
How should one write the following “conditional interlocked” function in the new C++ atomic<> style?
[...]
The short answer is:
T old = a; while( old >= 0 && !a.compare_exchange_weak( old, old+val ) ) { }
For the longer answer, continue reading...