qt-interest@trolltech.com
[Top] [All Lists]

Re: What does "volatile" mean in mutil-thread programming?

Subject: Re: What does "volatile" mean in mutil-thread programming?
From: Werner Van Belle
Date: Wed, 20 Feb 2008 07:37:45 +0100
Kermit Mei wrote:
> Werner Van Belle wrote:
>> Werner Van Belle wrote:
>>   
>>> 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.
>>>>       
>>> It means that any thread accessing the variable should read it again and
>>> not rely on some form of caching.
>>> E.g. If you want to wait until the
>>> thread stopped you can write something like
>>>
>>> while (thread->stopped) sleep(1);
>>>     
>> Reading my own post, I meant:
>>
>> while (!thread->stopped) sleep(1);
>>
>> it does however make no difference in the explanation.
>>   
> But, if I defined as :
>        bool stopped;
> then what will be happen?

If stopped is non volatile the compiler might decide that it stopped
doesn't change it the loop and no longer read the variable and thus the
waiting loop would never return even if the stopepd variable changed.

-- 
Dr. Werner Van Belle
http://werner.yellowcouch.org/

<Prev in Thread] Current Thread [Next in Thread>