Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Alexander Terekhov Newsgroups: comp.programming.threads Subject: Re: Is volatile so bad? Date: Mon, 02 Jan 2012 15:49:41 +0100 Organization: A noiseless patient Spider Lines: 33 Message-ID: <4F01C405.C964D503@web.de> References: <2012010123141758633-madler@alumnicaltechedu> Reply-To: terekhov@web.de Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Info: mx04.eternal-september.org; posting-host="MCbSEzW5qwGLFMNXJh39IQ"; logging-data="24163"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19CEOC/voWmThbXsRQz2Xw9LG7R9kMnJZc=" X-Mailer: Mozilla 4.77 [en] (Windows NT 5.0; U) X-Accept-Language: en Cancel-Lock: sha1:Do2aS/FW1Jq9LZxI8gAPqUMopuU= Xref: x330-a1.tempe.blueboxinc.net comp.programming.threads:807 Mark Adler wrote: > > My parallel gzip, pigz (http://zlib.net/pigz/) is written in C, uses > pthreads, and everything seems to work fine. Until recently the word > "volatile" did not appear in the code. > > However I got an email from a user about a global variable (local to a > single source file) that he said needed to be volatile. The variable > is a pointer to the head of a linked list of jobs, and access to it is > locked by a related pthreads mutex. The user said: > > "Without volatile the HP C compiler is doing a prefetch on this > variable before possess/enter_mutex and uses the prefetched old value > from a processor register still after the mutex." > > ("possess" is the name of a routine that calls pthread_mutex_lock.) > > So I figured what the heck, it can't hurt too much so I put the > volatile in there. The intent is that pigz be portable over a wide > range of operating systems, so I generally try to accommodate requests > like this. If your program is data-race-free then just file a bug report against HP C (it is really hard to believe that HP C can't deal with locking correctly). For use of volatile, see also: http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/user-faq.html regards, alexander.