From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21877 invoked by alias); 21 Nov 2013 08:55:24 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 21868 invoked by uid 89); 21 Nov 2013 08:55:23 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_50,RDNS_NONE,URIBL_BLOCKED autolearn=no version=3.3.2 X-HELO: mail2-relais-roc.national.inria.fr Received: from Unknown (HELO mail2-relais-roc.national.inria.fr) (192.134.164.83) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (CAMELLIA256-SHA encrypted) ESMTPS; Thu, 21 Nov 2013 08:55:22 +0000 Received: from ip-1.net-81-220-33.lyon.rev.numericable.fr (HELO laptop-mg.local) ([81.220.33.1]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 21 Nov 2013 09:55:12 +0100 Date: Thu, 21 Nov 2013 09:20:00 -0000 From: Marc Glisse Reply-To: gcc-help@gcc.gnu.org To: gcc-help@gcc.gnu.org cc: Andrew Makhorin Subject: Re: how to disable optimization for particular assignment statements? In-Reply-To: Message-ID: References: <1385018681.2093.3.camel@corvax> User-Agent: Alpine 2.10 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-SW-Source: 2013-11/txt/msg00165.txt.bz2 On Thu, 21 Nov 2013, Marc Glisse wrote: > On Thu, 21 Nov 2013, Andrew Makhorin wrote: > >> Hello, >> >> I have a C code like this: >> >> int foo(void) >> { int phase; >> . . . >> phase = 1; >> phase = 2; >> phase = 3; >> . . . >> } >> >> In case of -O0 gcc generates machine instructions for every >> assignment 'phase = ...'. But in case of -O2 gcc does not generate >> instructions for some assigments. Of course, this is correct. However, >> is there any way to tell gcc that 'phase' object is inspected by another >> thread, so it should not remove such statements? > > volatile Well, volatile will prevent the operations from being removed. For proper synchronization with other threads, using atomic operations with the right synchronization parameter sounds better. http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html -- Marc Glisse