Note that there are some explanatory texts on larger screens.

plurals
  1. POC++0x atomic implementation in c++98 question about __sync_synchronize()
    text
    copied!<p>I have written the followin atomic template with a view to mimicing the atomic operations which will be available in the upcoming c++0x standard.</p> <p>However, I am not sure that the __sync_synchronize() call I have around the returning of the underlying value are necessary. </p> <p>From my understanding, __sync_synchronize() is a full memory barrier and I'm not sure I need such a costly call when returning the object value.</p> <p>I'm pretty sure it'll be needed around the setting of the value but I could also implement this with the assembly ..</p> <pre><code>__asm__ __volatile__ ( "rep;nop": : :"memory" ); </code></pre> <p>Does anyone know wether I definitely need the synchronize() on return of the object.</p> <p>M.</p> <pre><code>template &lt; typename T &gt; struct atomic { private: volatile T obj; public: atomic( const T &amp; t ) : obj( t ) { } inline operator T() { __sync_synchronize(); // Not sure this is overkill return obj; } inline atomic&lt; T &gt; &amp; operator=( T val ) { __sync_synchronize(); // Not sure if this is overkill obj = val; return *this; } inline T operator++() { return __sync_add_and_fetch( &amp;obj, (T)1 ); } inline T operator++( int ) { return __sync_fetch_and_add( &amp;obj, (T)1 ); } inline T operator+=( T val ) { return __sync_add_and_fetch( &amp;obj, val ); } inline T operator--() { return __sync_sub_and_fetch( &amp;obj, (T)1 ); } inline T operator--( int ) { return __sync_fetch_and_sub( &amp;obj, (T)1 ); } inline T operator-=( T ) { return __sync_sub_and_fetch( &amp;obj, val ); } // Perform an atomic CAS operation // returning the value before the operation inline T exchange( T oldVal, T newVal ) { return __sync_val_compare_and_swap( &amp;obj, oldval, newval ); } }; </code></pre> <p>Update: I want to make sure that the operations are consistent in the face of read/write re-ordering due to compiler optimisations.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload