Optimization of conditional access to globals: thread-unsafe?
Ian Lance Taylor
iant@google.com
Tue Oct 30 14:50:00 GMT 2007
Tomash Brechko <tomash.brechko@gmail.com> writes:
> Even if we put aside the fact that there's no such membarrier()
> equivalent in POSIX bindings, this won't help.
In POSIX, any mutex function must be a membarrier. For example, on
x86, mutex lock and unlock more or less have to execute the mfence
instruction. If they don't, the program can see inconsistent data
structures despite the mutex operations.
> if (condition) {
> *p = value;
> membarrier();
> } else {
> membarrier();
> }
>
> But this is the same as
>
> if (condition)
> *p = value;
> membarrier();
No, it isn't. If membarrier is not a general function call, then it
has to be a magic function. In gcc it is implemented using a volatile
asm.
Note that I've committed my patch to avoid speculative stores to all
active branches, so this particular case should be a non-issue going
forward. However, we all are going to have to take a careful look at
gcc to make sure that it generally conforms to the C++0x memory model.
Ian
More information about the Gcc
mailing list