On 7/31/06, David Leung <[EMAIL PROTECTED]> wrote:
I am not a regular GCC user, but my recent experience with it has been quite
bitter. Recently I came across a piece of code that only produces correct
results with optimization turned on.

Yeah, that's not an uncommon result of code that doesn't meet the
synchronization requirements of pthreads.


My colleague has, by accident, compiled a piece of code we are working on
without any optimization, and we notice that the result produced by the
unoptimized code is incorrect. As I trace through the code, I found a simple
synchronization problem with an external variable being written to by the a
number of threads concurrently.

Right, so the code caused undefined behavior and the compiler happened
to do the desired thing only when optimization was on.  Did you not
see that same effect with other compilers on that code?


...
I'm becoming slightly more cynical about testing any piece of C code with
optimization turned on in GCC.

What are you testing for?  Correct operation of the resulting binary
or correctness of the code itself?

Note that other cases of undefined behavior (say, violation of C's
aliasing rules) are likely to behave 'correctly' when optimization is
off.


Philip Guenther

Reply via email to