__sync_fetch

Ian Lance Taylor iant@google.com
Sun Nov 18 08:07:00 GMT 2012


On Sat, Nov 17, 2012 at 11:04 PM, Hei Chan <structurechart@yahoo.com> wrote:
>
> After searching more for info, it seems like even though on a
>  64-bit machine, reading a long (i.e. 8 bytes) is one operation, it
> might not give the "correct" value:
> http://gcc.gnu.org/ml/gcc/2008-03/msg00201.html
>
> And so, we have to use __sync_fetch_and_add(&x, 0) to read?
>
> Could
>  someone elaborate a situation that reading a long variable won't get
> the correct value given that all writes in the application use
> __sync_fetch_*()?

If you always use __sync_fetch_and_add(&x, 0) to read a value, and you
always use __sync_fetch_and_add to write the value also with some
appropriate detla, then all the accesses to that variable should be
atomic with sequential consistency.  That should be true on any
processors that implements __sync_fetch_and_add in the appropriate
size.

Ian



More information about the Gcc-help mailing list