This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
gcc reordering
- From: Brian Modra <brian at zwartberg dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Fri, 31 Jul 2009 12:06:51 +0200
- Subject: gcc reordering
Hi,
Is it possible that the gcc optimiser could re-order the following
code:
sem_wait(&mutex);
bool different = val != buf_level;
sem_post(&mutex);
if (different) {
...
e.g. to:
sem_wait(&mutex);
sem_post(&mutex);
if (val != buf_level) {
...
???
I've asked this question generally on comp.lang.c++.moderated
The answer I have received is that although the standard does not
specifically forbifd it, it won't be happening because it knows that
code between sem_wait() and sem_post() need to stay within the
"locked block". I've also been told that I can always look at the
produced assembly, but that does not really help me, because there are
thousands of far more complicated bits of code I've written using
semaphores. I don't know what actually causes the optimiser
re-ordering to kick in.
The following re-oredring would also be "bad" for me, because
conditional block should be able to be run in a number of threads at
once, but the test only, should be synchronised:
sem_wait(&mutex);
bool different = val != buf_level;
if (different) {
...
}
sem_post(&mutex);
(This is because otherwise deadlocks can happen).
I will appreciate your reply, thanks.
Brian
--
Brian Modra Land line: +27 23 5411 462
Mobile: +27 79 69 77 082
5 Jan Louw Str, Prince Albert, 6930
Postal: P.O. Box 2, Prince Albert 6930
South Africa
http://www.zwartberg.com/