This is the mail archive of the
mailing list for the GCC project.
Re: Information on atomics in the gnu extensions: is a dereference atomic?
- From: Torvald Riegel <triegel at redhat dot com>
- To: Leo Ferres <leoferres at gmail dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Tue, 29 Apr 2014 17:16:30 +0200
- Subject: Re: Information on atomics in the gnu extensions: is a dereference atomic?
- Authentication-results: sourceware.org; auth=none
- References: <CACF6LVfdVEa6KXQYyNugjLcpUPQF-LmUqoJfL8fRaoy=cwOQ2A at mail dot gmail dot com>
On Tue, 2014-04-29 at 09:11 -0400, 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?
The atomic operation is on a single memory location, so in this example
only on the address you supply.
> other words, can a thread come between the dereferencing operation and
> the atomic increment of another thread? If so, is there a solution
> besides whileing with a compare and swap?
That depends on what the other threads are doing. If you need
background on concurrent programming, I suggest consulting a textbook
such as "The Art of Multiprocessor Programming" by Herlihy and Shavit.
How to solve this is not specific to C11 atomics or GCC's built-ins.
(Also, gcc-help@ would be a more appropriate list for this kind of