|
|
Here's the completely general answer:
A variable declared volatile can change it's value at any time, so the
compiler must not optimize it in any way.
Here's the simplest example I can come up with:
volatile int v;
...
int a = v + 1;
int b = v + 2;
In this case, the compiler must not optimize this to read v into a register,
because the value of v could change between the two assignments.
You can get this when your variable has some hardware connection. For example,
if you have a hardware random number generator, it's obvious that you want it
to give you different values for a and b above.
In multithreaded programming, this can happen when more than one thread has
write access to a variable. In the case above, another thread could write to
v between the assigments to a and b.
I hope this helps.
I guess my next post should be about the mutable storage modifier :)
Bo.
On onsdag den 20. Februar 2008, Kermit Mei wrote:
> Hello all!
> In embedded C++ code, the keyword "volatile" can prevent the complier
> from optimizing it.
> But, in mutil-thread programming, what dose it mean?
> Look the following code as an example, I wish you would like to explain
> it for me.
> #ifndef THREAD_H
> #define THREAD_H
>
> #include <QThread>
>
> class Thread : public QThread
> {
> Q_OBJECT
>
> public:
> Thread();
>
> void setMessage(const QString &message);
> void stop();
>
> protected:
> void run();
>
> private:
> QString messageStr;
> volatile bool stopped;
> };
>
> #endif
>
>
> Thanks!
>
> Kermit
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/
--
Thorsen Consulting ApS - Qt consulting services
http://www.thorsen-consulting.dk
--
To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
"unsubscribe" in the subject or the body.
List archive and information: http://lists.trolltech.com/qt-interest/
|
|