Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > volatile

Reply
Thread Tools

volatile

 
 
Marc Schellens
Guest
Posts: n/a
 
      04-27-2004
When has a variable to be declared volatile?
I have a program wich sets the variable through an
interrupt. But its read only from within the same program.
What if it would be multi-threaded?
thanks,
marc

 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      04-27-2004
On Tue, 27 Apr 2004 17:46:33 +0900, Marc Schellens
<> wrote:

>When has a variable to be declared volatile?


When it might be modified asynchronously by something outside of the
compiler's control, such as some hardware or another process (e.g.
shared memory). Also, atomic variables (using platform specific atomic
functions) should generally be volatile, at least on Win32, but you
may need memory barriers in addition to ensure that a read value is
the latest and a written value propogates to other processors on a
multiprocessor system.

>I have a program wich sets the variable through an
>interrupt. But its read only from within the same program.


If you set the value from an ISR and then read it from elsewhere in
the code, then usually you should make the variable volatile, but your
platform documentation should make this clear.

>What if it would be multi-threaded?


In general, volatile is neither necessary or sufficient for
thread-safety. You need locking primitives (mutexes/critical sections)
to safely share resources, or atomic primitives for single variables.
All this is platform dependent though (although a lot of platforms
have standardised on POSIX).

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      04-27-2004
On Tue, 27 Apr 2004 11:28:45 +0100, tom_usenet
<> wrote:

>On Tue, 27 Apr 2004 17:46:33 +0900, Marc Schellens
><> wrote:
>
>>When has a variable to be declared volatile?


I should mention that some of the experts on this hang out in
comp.programming.threads

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
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
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0xffc Thread 0x228 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 0 03-30-2006 03:16 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 1 03-24-2006 04:20 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
Re: Volatile? Knute Johnson Java 17 07-03-2003 03:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57