From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17073 invoked by alias); 21 Oct 2007 23:07:57 -0000 Received: (qmail 17064 invoked by uid 22791); 21 Oct 2007 23:07:57 -0000 X-Spam-Check-By: sourceware.org Received: from mail.artimi.com (HELO mail.artimi.com) (194.72.81.2) by sourceware.org (qpsmtpd/0.31) with ESMTP; Sun, 21 Oct 2007 23:07:54 +0000 Received: from rainbow ([192.168.8.46]) by mail.artimi.com with Microsoft SMTPSVC(6.0.3790.3959); Mon, 22 Oct 2007 00:07:51 +0100 From: "Dave Korn" To: "'Tomash Brechko'" , References: <20071021145513.GB4925@moonlight.home> Subject: RE: Optimization of conditional access to globals: thread-unsafe? Date: Sun, 21 Oct 2007 23:07:00 -0000 Message-ID: <00cd01c81437$34ada090$2e08a8c0@CAM.ARTIMI.COM> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <20071021145513.GB4925@moonlight.home> Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2007-10/txt/msg00270.txt.bz2 On 21 October 2007 15:55, Tomash Brechko wrote: > Consider this piece of code: > > extern int v; > > void > f(int set_v) > { > if (set_v) > v = 1; > } > f: > pushl %ebp > movl %esp, %ebp > cmpl $0, 8(%ebp) > movl $1, %eax > cmove v, %eax ; load (maybe) > movl %eax, v ; store (always) > popl %ebp > ret > > Note the last unconditional store to v. > So, could someone explain me why this GCC optimization is valid, 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 you really want all externally-visible accesses to v to be made exactly as the code directs, rather than allowing gcc to optimise them in any way that (from the program's POV) it's just the same 'as-if' they had been done exactly, make v volatile. cheers, DaveK -- Can't think of a witty .sigline today....