Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: how to use volatile key word? |
| Date | 2012-06-26 12:57 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <jscplj$8ig$1@dont-email.me> (permalink) |
| References | <4fa787bc-0930-4c4e-a1e9-dbf94b909e8a@googlegroups.com> |
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
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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