volatile

closed account (28poGNh0)
Can someone give me an expample when you realy see the effect of volatile keyword?

thanks
An extremely contrived example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

int main()
{
    int x = 5000;
    int y = 1;
    while(x == 5000)
    {
        ++y;
        if(y == 500000000)
        {
            break;
        }
        while(x == 5000)
        {
            ++y;
            break;
        }
    }
    y = 0;
    std::cout << "done.";
    return 0;
}


Run it a few times. On my laptop it executes in about 0.2s. Then change line 5 to:
 
volatile int x = 5000; 


You will see the execution time increase, as the compiler will not optimize the loops x is in to while(true) loops (because we have told it not to). For me, the execution time goes up to around 0.9s.
closed account (28poGNh0)
It still the same in mine 1.2s with/without volatile ,and I still dont get why this keyword exist

but thanks anyway for the repley
closed account (o3hC5Di1)
In addition to Mats's example, following answer contains the explanation behind it: http://stackoverflow.com/a/4437555

All the best,
NwN
@techno01 - Turn on all your compiler optimizations. It's running at 1.2s because your compiler is not attempting to optimize the code.
Last edited on
closed account (28poGNh0)
Turn on all your compiler optimizations. It's running at 1.2s because your compiler is not attempting to optimize the code.


1st
@Mats how can I
Turn on all my compiler optimizations


@NwN
This is a greate thread http://stackoverflow.com/a/4437555
two questions though

first :
because it may be that someone else is changing the value of some_int from outside the program which compiler is not aware of

how can someone changes some_int from outside the program

second :
the compiler may optimize this code

is the compiler do such things ,please can you tell how ?(e.i how the compiler find out the the some_int not gonna get changed,is It examined the whole code to do the optimization ,I hope I express well)

Thanks a lot
The primary reason this keyword exists is to support memory-mapped I/O, when accessing some system-supplied memory address is routed, by hardware, to an external device. You won't see much of that in application code.

Another use is sharing data with an async signal handler that executes on the same thread (although it is technically a side-effect of its specification, and in C++11, std::atomic_signal_fence exists for this purpose)

And yes, it is occasionally useful to disable certain optimizations. Besides microbenchmarks, this is useful for example, if your compiler doesn't support FENV_ACCESS.

Last edited on
@Techno01 - It depends what you're using to compiler the code. I'm using mingw under CodeBlocks and have to do - Settings -> compiler -> click checkbox 'optimize fully for speed'. I imagine it's something similar on other environments.

how can someone changes some_int from outside the program


There are many ways of changing an int from outside the program. For example, I might be compiling this small source file to become part of a much larger project. In the larger project, the value might be changed.

is the compiler do such things ,please can you tell how ?(e.i how the compiler find out the the some_int not gonna get changed,is It examined the whole code to do the optimization ,I hope I express well)


I had a look at compiler optimization earlier this year - It's a very large topic! I soon realized I had better things to be doing than trying to figure out exactly how compilers optimize things.
closed account (28poGNh0)
@Mats thanks for the great explanation

I had a look at compiler optimization earlier this year - It's a very large topic! I soon realized I had better things to be doing than trying to figure out exactly how compilers optimize things.


fair enough

It depends what you're using to compiler the code. I'm using mingw under CodeBlocks and have to do - Settings -> compiler -> click checkbox 'optimize fully for speed'. I imagine it's something similar on other environments.


I am using code::blocks too ,I am gonna do what suggest later

now the main thing

There are many ways of changing an int from outside the program. For example, I might be compiling this small source file to become part of a much larger project. In the larger project, the value might be changed.


I am afraid i need more explanation
lets say the I have a small program

# include <iostream>
using namespace std;

int main()
{
int nbr = 5;

cout << nbr << endl;

return 0;
}

It is simple

now how can I change the nbr to 6 from outside the program so it can output 6(if that is possible)?
can you tell me how can I do it?
@Techno - Cubbi already answered:
The primary reason this keyword exists is to support memory-mapped I/O, when accessing some system-supplied memory address is routed, by hardware, to an external device. You won't see much of that in application code.
Last edited on
volatile is used to make sure that all writes to the variable will actually perform memory writes... and all reads from that variable will actually perform memory reads.

Without 'volatile', these reads/writes are often omitted by the optimizer because the variable can be kept in CPU registers. This allows for faster code.

However... when dealing with hardware communication... the physical reads/writes may be necessary in order to trigger certain events in the hardware. Therefore, you'd use the volatile keyword to prevent the compiler from optimizing away the reads/writes... to ensure that the hw actually gets read from/written to.


So yeah... unless you are doing low level HW interaction, you generally do not need to (and shouldn't) use the volatile keyword.
Last edited on
closed account (28poGNh0)
Man this is so much beyond my understanding ,I'll opened this thread later as learn some of your alian words .. however Thanks a lot all of you

@Mats ,@NwN ,@Cubbi and @Disch
Topic archived. No new replies allowed.