You Can Do Any Kind of Atomic Read-Modify-Write Operation
preshing.com
preshing.com
When spinning on atomics, the interconnect between cores needs to do a lot of work synchronizing the memory. When adding a pause or monitor instruction, the core will give an opportunity for the other hyperthread in the same core to execute, reducing the amount of churn on the interconnect.
Counterintuitively, adding a "pause" in the spin loop will make the program faster by yielding the CPU core for a few nanoseconds.
uint32_t fetch_multiply(std::atomic<uint32_t>& shared, uint32_t multiplier)
{
uint32_t oldValue = shared.load();
while (!shared.compare_exchange_weak(oldValue, oldValue * multiplier))
{
}
return oldValue;
}
If shared.compare_exchange_weak() fails because of a concurrent writer, then the while loop will never exit (unless another writer sets the value of shared to oldValue).What the author wants is something like this:
uint32_t fetch_multiply(std::atomic<uint32_t>& shared, uint32_t multiplier)
{
while(1) {
uint32_t oldValue = shared.load();
if(shared.compare_exchange_weak(oldValue, oldValue * multiplier)) {
return oldValue;
}
}
}
The other examples have the same problem.This is an issue with multi threaded programming. Only way to test is to run stress testing, with various simulated payloads, for minutes at a time, preferably on different cpu architectures.
I've been bit by this before. I sometimes wish you had to mark "reference-of" just like you have to mark "address-of" with &.
It's obvious that blah(&foo) might modify foo, without needing to examine the signature of blah().
The tradeoff is a bit of code clutter when you add a symbol (something like shared.compare_exchange_weak(%oldValue, oldValue * multiplier)), and a violation of DRY, since the information provided by this symbol would be redundant.
Atomically, compares the contents of the memory pointed to by object or by this for equality with that in expected, and if true, replaces the contents of the memory pointed to by object or by this with that in desired, and if false, updates the contents of the memory in expected with the contents of the memory pointed to by object or by this.
http://open-std.org/JTC1/SC22/WG21/docs/papers/2011/n3242.pd...
Atomically compares the object representation of this with the object representation of expected, as if by std::memcmp, and if those are bitwise-equal, replaces the former with desired (performs read-modify-write operation). Otherwise, loads the actual value stored in this into expected (performs load operation). Copying is performed as if by std::memcpy.
http://en.cppreference.com/w/cpp/atomic/atomic/compare_excha...
You still rely on ability to locally order operations in time (i.e., you have to be sure that you - the CPU - reads from that memory cell strictly after it completed writing into this one) - so there are restrictions for modern CPUs. You might have issues if you want to synchronize many threads/processes - as the locking delay may become too long. But still.