Tomash Brechko writes: > On Mon, Oct 22, 2007 at 00:07:50 +0100, Dave Korn wrote: > > Because of the 'as-if' rule. Since the standard is neutral > > with regard to threads, gcc does not have to take them into > > account when it decides whether an optimisation would satisfy the > > 'as-if' rule. > > If this would be true, then the compiler is free to inject the > sequence > > mov mem -> reg > mov reg -> mem > > just _anywhere_.
That's right. This isn't a standards conformance issue, rather one of quality of implementation. The core problem here seems to be that the "C with threads" memory model isn't sufficiently well-defined to make a determination possible. You're assuming that you have no resposibility to mark shared memory protected by a mutex as volatile, but I know of nothing in the C standard that makes such a guarantee. A prudent programmer will make conservative assumptions. Please have a read of [1]. Let us know if anything you have observed isn't covered in that paper. Andrew. [1] Hans-Juergen Boehm. Threads cannot be implemented as a library. In Proc. of the ACM SIGPLAN 2005 Conf. on Programming Language Design and Implementation (PLDI), pages 261?268, Chicago, IL, June 2005.