Information on atomics in the gnu extensions: is a dereference atomic?
Andrew Haley
aph@redhat.com
Tue Apr 29 13:42:00 GMT 2014
[Redirect to gcc-help]
Hi,
On 04/29/2014 02:11 PM, Leo Ferres wrote:
> Suppose the following C code.
>
> int t = __sync_fetch_and_add(&s->r, 1);
>
> s is a struct with member r, which is an volatile int.
>
> Is this an atomic operation, even if you include the indirection? In
> other words, can a thread come between the dereferencing operation and
> the atomic increment of another thread?
Yes. Or rather, which dereference? There are two.
> If so, is there a solution besides whileing with a compare and swap?
>
> What about stdatomic.h in C11, is there a way to do what I want there?
You need an atomic operation which covers a CAS and a fetch. Unless you
want to use transactional memory you're going to have to use a lock.
Andrew.
More information about the Gcc-help
mailing list