gccc++11transactionsatomicatomic-swap

How to atomically perform sequential load and store operations?


Consider this code under GCC 4.8.0:

std::atomic<bool> a;
std::atomic<bool> b;

a.store( b.load() ); // want to be atomic

How can I make the line above to be atomic as whole? In other words, how to obtain atomic assignment of atomic variables?

Are there any alternatives for std::atomic which allow this?


I have found __transaction_atomic {/* any code goes here */} which is activated on GCC by -fgnu-tm. With this, one can write anything in the block and it will be performed atomically.

Now the question are:

Is __transaction_atomic implemented with mutexes? If yes, then what the mutex actually locks?

Does the implementation of __transaction_atomic change depending on what is in it's block? If yes, then how it changes?


Solution

  • In theory atomic variable swap could be implemented on a few CPUs with DCAS support. In practice no modern CPU has DCAS support, so it's not possible.