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


Groups > comp.lang.c > #22809

Re: how to use volatile key word?

From Xavier Roche <xroche@free.fr.NOSPAM.invalid>
Newsgroups comp.lang.c
Subject Re: how to use volatile key word?
Date 2012-06-26 18:56 +0200
Organization Nowhere Corp.
Message-ID <jscpkf$fl9$1@news.httrack.net> (permalink)
References <4fa787bc-0930-4c4e-a1e9-dbf94b909e8a@googlegroups.com>

Show all headers | View raw


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.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web