Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to use volatile key word?

Reply
Thread Tools

how to use volatile key word?

 
 
伏虎
Guest
Posts: n/a
 
      06-26-2012
how to use volatile key word?

Assume I have a global variable shared by two or more tasks. Do I need to declare it as volatile?

When will a non-volatile global variable be written back to memory from a register copy? Before a function call? Or before the return statement?



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-26-2012
On 6/26/2012 12:11 PM, 伏虎 wrote:
> how to use volatile key word?
>
> Assume I have a global variable shared by two or more tasks. Do I
> need to declare it as volatile?


'volatile' usually means that the object's value can be changed at any
moment by some mechanism outside of your program control. When you have
a global variable shared between parts of *your program*, that's all
under your control, and hence doesn't need to be declared 'volatile'.

> When will a non-volatile global variable be written back to memory
> from a register copy? Before a function call? Or before the return
> statement?


Side effects (like changing memory) happen at "sequence points", within
the same thread. Shared [between threads] data need to be protected
better, and there are mechanisms for that - see "atomic operations" and
"synchronization". That's all in theory, of course. Do you have a
particular problem?

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      06-27-2012
Paavo Helde <(E-Mail Removed)> wrote:
> Volatile is meant to be used for accessing memory-mapped hardware, if you
> are not writing device drivers or such you can just forget about it. It is
> neither needed nor sufficient for portable thread synchronisation.


'volatile' might not be sufficient for thread synchronization nor atomicity,
but it can certainly make a difference in a multithreaded program.

In a program I had a case where I was just reading an integral from one
thread that was changed by another thread to signal a minor, unimportant
effect (namely something related to updating the UI). This did not require
full-fledged locking (which would have made it needlessly expensive) because
if the integral had a wrong value for a split second, that wasn't really
a catastrophical event.

However, 'volatile' had a major impact on that integral. Without it, the
other thread was not seeing the changes immediately, causing a noticeable
delay (it was a situation where the thread that was setting that integral
did it inside a loop, and the compiler was optimizing the updating of the
variable in such a way that the actual writing to the extern variable was
delayed to be after the loop). With 'volatile' the variable was always
updated immediately.
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      06-27-2012
On Tue, 26 Jun 2012 13:53:23 -0500, Paavo Helde
<(E-Mail Removed)> wrote:

>=?UTF-8?B?5LyP6JmO?= <(E-Mail Removed)> wrote in
>news:(E-Mail Removed):
>
>> how to use volatile key word?

>
>Volatile is meant to be used for accessing memory-mapped hardware, if you
>are not writing device drivers or such you can just forget about it. It is
>neither needed nor sufficient for portable thread synchronisation.


I'd say the volatile keywork is not sufficient but it is necessary in
some circumstances.

>See e.g. http://en.wikipedia.org/wiki/Volatile_variable


This page does show cases where use of the volatile keyword is
necessary.
--
(\__/) M.
(='.'=) If a man stands in a forest and no woman is around
(")_(") is he still wrong?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-27-2012
On 06/27/12 05:32 PM, Juha Nieminen wrote:
> Paavo Helde<(E-Mail Removed)> wrote:
>> Volatile is meant to be used for accessing memory-mapped hardware, if you
>> are not writing device drivers or such you can just forget about it. It is
>> neither needed nor sufficient for portable thread synchronisation.

>
> 'volatile' might not be sufficient for thread synchronization nor atomicity,
> but it can certainly make a difference in a multithreaded program.
>
> In a program I had a case where I was just reading an integral from one
> thread that was changed by another thread to signal a minor, unimportant
> effect (namely something related to updating the UI). This did not require
> full-fledged locking (which would have made it needlessly expensive) because
> if the integral had a wrong value for a split second, that wasn't really
> a catastrophical event.


Did you measure? In the absence of contention, the impact of locking
should be close to none.

--
Ian Collins
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      06-28-2012
Paavo Helde <(E-Mail Removed)> wrote:
> Juha Nieminen <(E-Mail Removed)> wrote in
>> In a program I had a case where I was just reading an integral from
>> one thread that was changed by another thread to signal a minor,
>> unimportant effect (namely something related to updating the UI). This
>> did not require full-fledged locking (which would have made it
>> needlessly expensive) because if the integral had a wrong value for a
>> split second, that wasn't really a catastrophical event.

>
> Out of curiosity - did you measure how much the "needlessly expensive"
> locking was slower than using a volatile? I'm asking because I find myself
> often in urge to invent all kind of clever tricks to bypass proper locking.


No, but it was enough that the options were between writing 'volatile'
and several lines of code using a non-standard library to implement proper
locking, so the choice was rather clear to me.

(In C++11 it might have been enough to declare the integral to be atomic,
and it would probably have been equally fast and fully synchronized. However,
I did not have C++11 at my disposal at that time.)
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      06-28-2012
Ian Collins <(E-Mail Removed)> wrote:
> Did you measure? In the absence of contention, the impact of locking
> should be close to none.


Locking is always expensive, even if no thread has to wait at any point.

That's the very reason why so much research is being done on lock-free
containers and atomic operations.

Anyways, if the choice is between writing 'volatile' and writing several
lines of code to implement locking in a situation where locking is not
needed, the choice is pretty clear.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-28-2012
On 06/28/12 05:55 PM, Juha Nieminen wrote:
> Ian Collins<(E-Mail Removed)> wrote:
>> Did you measure? In the absence of contention, the impact of locking
>> should be close to none.

>
> Locking is always expensive, even if no thread has to wait at any point.
>
> That's the very reason why so much research is being done on lock-free
> containers and atomic operations.
>
> Anyways, if the choice is between writing 'volatile' and writing several
> lines of code to implement locking in a situation where locking is not
> needed, the choice is pretty clear.


Until you have to run on a multi-core system....

--
Ian Collins
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      06-28-2012
On Wed, 27 Jun 2012 16:12:05 -0500, Paavo Helde
<(E-Mail Removed)> wrote:

>Mark <(E-Mail Removed)> wrote in
>news:(E-Mail Removed) :
>
>>
>>>See e.g. http://en.wikipedia.org/wiki/Volatile_variable

>>
>> This page does show cases where use of the volatile keyword is
>> necessary.

>
>Not sure what you mean, for C and C++ I see only discussion about memory-
>mapped hardware. The examples about multi-thread synchronization with
>volatile are talking about Java and C# where this keyword apparently means
>something different.


As I understand it there is still a risk of the unsafe opimizations
which the volatile keyword would prevent.
--
(\__/) M.
(='.'=) If a man stands in a forest and no woman is around
(")_(") is he still wrong?

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      06-28-2012
Ian Collins <(E-Mail Removed)> wrote:
>> Anyways, if the choice is between writing 'volatile' and writing several
>> lines of code to implement locking in a situation where locking is not
>> needed, the choice is pretty clear.

>
> Until you have to run on a multi-core system....


What difference would that make?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to use volatile key word? 伏虎 C++ 1 07-02-2012 08:31 PM
how to use volatile key word? 伏虎 C Programming 4 06-27-2012 05:28 PM
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x8fc Thread 0x934 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 0 03-24-2006 02:22 PM
Use of the Volatile keyword for a pointer to a volatile memory block ben C Programming 5 01-11-2005 05:38 PM



Advertisments