Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #22807 > unrolled thread

how to use volatile key word?

Started by伏虎 <fuhu314@hotmail.com>
First post2012-06-26 09:10 -0700
Last post2012-06-27 10:28 -0700
Articles 7 — 7 participants

Back to article view | Back to comp.lang.c


Contents

  how to use volatile key word? 伏虎 <fuhu314@hotmail.com> - 2012-06-26 09:10 -0700
    Re: how to use volatile key word? Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2012-06-26 18:56 +0200
    Re: how to use volatile key word? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-26 12:57 -0400
    Re: how to use volatile key word? Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-26 21:06 +0200
    Re: how to use volatile key word? David Brown <david@westcontrol.removethisbit.com> - 2012-06-27 09:08 +0200
      Re: how to use volatile key word? Tim Wescott <tim@seemywebsite.com> - 2012-06-27 11:10 -0500
    Re: how to use volatile key word? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-27 10:28 -0700

#22807 — how to use volatile key word?

From伏虎 <fuhu314@hotmail.com>
Date2012-06-26 09:10 -0700
Subjecthow to use volatile key word?
Message-ID<4fa787bc-0930-4c4e-a1e9-dbf94b909e8a@googlegroups.com>
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?


[toc] | [next] | [standalone]


#22809

FromXavier Roche <xroche@free.fr.NOSPAM.invalid>
Date2012-06-26 18:56 +0200
Message-ID<jscpkf$fl9$1@news.httrack.net>
In reply to#22807
Le 26/06/2012 18:10, 伏虎 a écrit :
> how to use volatile key word?

[ I can not find the exact C spec extract for volatile ; maybe someone
will complete this approximative message ]

volatile is meant to access part of memory which can be modified outside
the scope of the current thread (by another thread, for example, or by a
signal handler in the same thread ; it can be mapped memory, or
hardware-mapped memory too)

"volatile" in C is not intended for synchronization purpose (ie. unlike
in java). It will _not_ cause any memory barriers, nor it will ensure
operations are atomical.

You can see it as a strong hint to the compiler: "do not attempt to make
optimizations on this variable" ; such as in this case:

extern int spin = 0;

void wait_spin() {
  while(!spin) ;
}

void set_spin() {
  spin = 1;
}

In this case, a sane compiler will optimize wait_spin() into:

void wait_spin() {
  if (!spin) {
    for(;;) ;
  }
}

.. leading to an infinite loop.

By declaring spin "volatile", the compiler will not assume that "spin"
can not be modified inside the scope if wait_spin().

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

No. You should properly synchronize it, for example using mutexes.

> 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?

If it is volatile, it will be read/written immediately, the compiler is
not supposed to alias it to a register. (except if the operation is not
atomical and is processed using a temporary register). However, once
again, synchronization and side effects propagating will be totally
unspecified.

[toc] | [prev] | [next] | [standalone]


#22810

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-06-26 12:57 -0400
Message-ID<jscplj$8ig$1@dont-email.me>
In reply to#22807
On 6/26/2012 12:10 PM, 伏虎 wrote:
[... reformatted for line length ...]
> 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?

     That will depend on what you mean by "tasks," and on how you
make the variable "shared."  If the two "tasks" are executing
different programs, anything you "share" between them is probably
in a shared memory segment or something of that kind, and has its
own rules for ensuring consistency.  If they are "threads" in the
same program, `volatile' is (probably) neither necessary nor
sufficient to guarantee consistency; I suggest you seek more
information in comp.programming.threads.

     (The very latest "C11" version of the C Standard includes
features to support multi-threaded programming, and may have more
to say on the matter.  However, I have not yet studied C11's
threading stuff in detail -- also, C11 is so new that few compilers
support it yet.)

> 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?

     It will be written back to memory when the implementation
chooses.  That's really all that can be said.

     Note that in a multi-threaded context, "written back to
memory" does not mean "Seen in a consistent state by all threads."
Most threading models require some kind of explicit synchronization
between threads that read and write the same object; merely "T1
wrote it, then eight nanoseconds elapsed before T2 read it" is
not enough to guarantee that T2 sees what T1 wrote, nor that T2
sees anything sensible at all.  Again, I'll refer you to
comp.programming.threads.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#22818

FromJens Gustedt <jens.gustedt@loria.fr>
Date2012-06-26 21:06 +0200
Message-ID<4FEA082C.9010809@loria.fr>
In reply to#22807
Am 26.06.2012 18:10, schrieb 伏虎:
> 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?

Using it in such a context is not sufficient. Even with volatile there
would be no guarantee that the compiler would load a consistent value
from memory. E.g in extreme cases it could load the upper half of a
variable in one load and and the lower half with another. Your program
then could see a mix of two different values.

The feature that you would be needing is C11's _Atomic and not
volatile. Many compilers already provide similar features as
extensions. P99 supports part of the C11 syntax on top of gcc and
cousins.

Jens

[toc] | [prev] | [next] | [standalone]


#22853

FromDavid Brown <david@westcontrol.removethisbit.com>
Date2012-06-27 09:08 +0200
Message-ID<FJCdnQHCbf30L3fSnZ2dnUVZ7tmdnZ2d@lyse.net>
In reply to#22807
On 26/06/2012 18:10, 伏虎 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?
>
> 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?
>
>
>


You have multi-posted this same question to multiple newsgroups - at 
least comp.lang.c, comp.lang.c++, and comp.arch.embedded.  This is bad 
practice - if you think your question belongs in all groups, you should 
cross-post a single post to all groups.  That way there will be a single 
discussion thread, and the embedded people and language lawyers can 
discuss together.  You are currently getting two sets of opinions as 
seen from wildly different viewpoints.

You must decide if you are talking about an embedded system or a 
PC-style system.  Are you talking about small, simple processors, or 
large multi-core systems?  Are you talking about interrupt tasks on a 
microcontroller or multiple threads with an OS?  Are you talking about 
small pieces of data (single variables) or large blocks of data? 
Depending on the answers, it could be "yes, volatile is all you need to 
share the data" up to "no, volatile is such a small part of what you 
need that it is not worth bothering about".

Once you have figured out the right question, and the right details, 
then re-post as a cross-post (or as just a single post, if you think 
only one group is appropriate).  Then we can help give you the right advice.

[toc] | [prev] | [next] | [standalone]


#22866

FromTim Wescott <tim@seemywebsite.com>
Date2012-06-27 11:10 -0500
Message-ID<7-6dnSTYNeYVrXbSnZ2dnUVZ_iydnZ2d@web-ster.com>
In reply to#22853
On Wed, 27 Jun 2012 09:08:08 +0200, David Brown wrote:

> On 26/06/2012 18:10, 伏虎 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?
>>
>> 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?
>>
>>
>>
>>
> 
> You have multi-posted this same question to multiple newsgroups - at
> least comp.lang.c, comp.lang.c++, and comp.arch.embedded.  This is bad
> practice - if you think your question belongs in all groups, you should
> cross-post a single post to all groups.  That way there will be a single
> discussion thread, and the embedded people and language lawyers can
> discuss together.  You are currently getting two sets of opinions as
> seen from wildly different viewpoints.
> 
> You must decide if you are talking about an embedded system or a
> PC-style system.  Are you talking about small, simple processors, or
> large multi-core systems?  Are you talking about interrupt tasks on a
> microcontroller or multiple threads with an OS?  Are you talking about
> small pieces of data (single variables) or large blocks of data?
> Depending on the answers, it could be "yes, volatile is all you need to
> share the data" up to "no, volatile is such a small part of what you
> need that it is not worth bothering about".
> 
> Once you have figured out the right question, and the right details,
> then re-post as a cross-post (or as just a single post, if you think
> only one group is appropriate).  Then we can help give you the right
> advice.

And if the terms "cross posting" and "multiple posting" mean nothing to 
you, check out this Wiki article: http://en.wikipedia.org/wiki/Cross_post.

I had been unaware that the question had been posted to multiple groups: 
yes, you will get wildly different answers, because what's appropriate 
and applicable for an itty bitty processor with code running on bare 
metal is considerably different -- and often diametrically opposed to -- 
what's appropriate and applicable in an application running on a desktop.

Consider a guy in camo pants with a pack full of freeze-dried food, armed 
with a long knife and a shotgun.  Put him in Times Square -- is his gear 
appropriate and applicable?  How about a lawyer in a shiny suit with $300 
shoes, armed with a Visa Platinum?  How's he doing?  Now put those two 
guys out in the sticks in Alaska -- who's gear makes sense now?

Ditto software practices in (deep) embedded vs. desktop.

-- 
My liberal friends think I'm a conservative kook.
My conservative friends think I'm a liberal kook.
Why am I not happy that they have found common ground?

Tim Wescott, Communications, Control, Circuits & Software
http://www.wescottdesign.com

[toc] | [prev] | [next] | [standalone]


#22874

FromTim Rentsch <txr@alumni.caltech.edu>
Date2012-06-27 10:28 -0700
Message-ID<kfn4npw64w3.fsf@x-alumni2.alumni.caltech.edu>
In reply to#22807
 <fuhu314@hotmail.com> writes:

> 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?

If your intention is to write code that runs on several platforms,
using volatile almost certainly isn't the right way to do that.
It is possible using volatile will help, but there is no guarantee
of that for multi-platform or cross-platform code.  Probably though
this isn't what you're setting out to do.

If you are writing code for a specific platform, or perhaps for a
small number of specific platforms, it's best to consult the
platform-specific documentation for how to do what you want.
That way you'll be better informed, and the information you
get is much more likely to be a reliable indicator of what's
needed to do what you want.  If that documentation says use
volatile, then use volatile;  if it says something else, do
that instead.  Also that documentation should let you answer
your other questions.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web