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
 
 
 
 
Joshua Maurice
Guest
Posts: n/a
 
      07-02-2012
On Jun 26, 9:13*am, 伏虎 <(E-Mail Removed)> 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 aregister copy? Before a function call? Or before the return statement?


How do you use the volatile keyword in C++? You don't.

Volatile has 3 valid, and "obscure", uses in C++ code: signal
handling, memory mapped IO
http://en.wikipedia.org/wiki/Memory-mapped_I/O
and setjmp longjmp stuff.

The volatile keyword in C and C++, for all standards, is completely
useless as a portable primitive for threading. Do not use it as such.
For example, if you tried to use it on any job interview I was giving,
I would not give you the job. Instead, use POSIX pthreads, windows
threads, C11 threads, C++11 threads, reliable wrappers on top of those
such as Boost or ACE (or C11 or C++11), write your own abstraction on
top of those, or so on. Use mutexes by default. Use weaker atomics
only when you understand enough to use them correctly.

To everyone else in this thread: No. Just no. volatile is entirely
useless as a portable threading primitive in C and C++. If I see any
code that attempts to use volatile as a portable construct for
synchronization, I will call it broken, buggy, and in need of
immediate fixing. However, yes you can use volatile on specific
hardware with specific compilers to accomplish working code. Note the
distinction between "specific hardware with specific compilers" vs
"portable". And if you do use volatile in the non-portable way, at
least put it inside an inline function or macro which implements the
"usual semantics" (i.e. C++11 acquire, release, etc.) to keep the evil
isolated, please.
 
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++ 24 08-03-2012 12:35 AM
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