Boost logo

Boost :

Subject: Re: [boost] New Book: Introduction to the C++ Boost Libraries, Volume I - Foundations
From: Daniel J. Duffy (dduffy_at_[hidden])
Date: 2010-09-22 07:55:05


 

> 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().

On my dual-core machine the version without volatile produces wrong results while the version with volatile produces correct results.

 Daniel

 

 

     
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost




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