Boost logo

Boost :

Subject: Re: [boost] New Book: Introduction to the C++ Boost Libraries, Volume I - Foundations
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2010-09-22 11:26:24


On Wed, Sep 22, 2010 at 1:13 PM, Artyom <artyomtnk_at_[hidden]> wrote:
>> > Part: 18.13 Volatile Variables
>>
>> > >  In order  to rectify this situation we declare variables to be volatile
>> > >   in order to disable register optimisations:
>> > >
>> > >   //  Shared variable
>> > >   volatile int i2;
>> > >   // The  same functions as above but use a volatile variable.
>> > >   // Not  optimised anymore so results are always correct.
>> > >   void FV1()  { while (true) i2++; }
>> > >   void FV2() { while (true)  cout<<i2<<endl; }
>> >
>> > This is so wrong!
>>
>> Please  explain why this is wrong. Is the code wrong? Is the example wrong?
>>Please note  that this is a snippet from the full test program where two
>>different threads  are started to execute FV1() and FV2().
>>
>
>
> In addition to what was told, read following.
>
> Read this: http://en.wikipedia.org/wiki/Volatile_variable#In_C_and_C.2B.2B
> And this:  http://kernel.org/doc/Documentation/volatile-considered-harmful.txt
>
> Artyom

And from the original author of POSIX Threads himself:

http://groups.google.com/group/comp.programming.threads/msg/bd2cb64e70c9d155

HTH,

-- 
gpd

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk