473,545 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

volatile variables

HI,
Can volatile variables be accessed by many processess..or only one
process can access it..

Cheers..

Aug 16 '05 #1
17 7039

"Radde" <ms******@gmail .com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
HI,
Can volatile variables be accessed by many processess..or only one
process can access it..

Cheers..


Many, it is just a variable not to be optimized away.

Ben
Aug 16 '05 #2
Can u be more eloborate on that..

Aug 16 '05 #3
The only difference between a volatile and a non-volatile variable, as far
as I know, is that the non-volatile one has a chance to be totally
eliminated by the optimizing compiler. A few operations does depend on the
existence of certain variables while the compiler can't deduce at compile
time, and that's why you need to tell the compiler so by using the
"volatile" keyword.

This has no direct connection with accessing the variable from multiple
processors. Of course, the variable needs to exists in order to be accessed.

ben
Aug 16 '05 #4
benben wrote:

The only difference between a volatile and a non-volatile variable, as far
as I know, is that the non-volatile one has a chance to be totally
eliminated by the optimizing compiler.


Well. Not exactly.
volatile tells the compiler that this variable can get altered by ways that
the compiler cannot deduce. As a consequence, the compiler will exclude that
variable from optimizations and the code will always fetch the variables content
from memory, even if it has done so before (no caching).

Imagine a hardware clock which is mapped into memory. You need some way to
tell the compiler: whenever I access that variable (mapped to the hardware
clock), I want you to actually fetch the content from there. Otherwise the
compiler might just ask the clock once for the current time and use the
cached value (in a processor register) throught the rest of the program.

--
Karl Heinz Buchegger
kb******@gascad .at
Aug 16 '05 #5
Karl Heinz Buchegger wrote:
benben wrote:
The only difference between a volatile and a non-volatile variable, as far
as I know, is that the non-volatile one has a chance to be totally
eliminated by the optimizing compiler.

Well. Not exactly.
volatile tells the compiler that this variable can get altered by ways that
the compiler cannot deduce. As a consequence, the compiler will exclude that
variable from optimizations and the code will always fetch the variables content
from memory, even if it has done so before (no caching).

Imagine a hardware clock which is mapped into memory. You need some way to
tell the compiler: whenever I access that variable (mapped to the hardware
clock), I want you to actually fetch the content from there. Otherwise the
compiler might just ask the clock once for the current time and use the
cached value (in a processor register) throught the rest of the program.


But it doesn't guarantee atomic reads. A non-atomic read of the clock could
be problematic.

--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Aug 16 '05 #6
"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message news:43******** *******@gascad. at...
volatile tells the compiler that this variable can get altered by ways that
the compiler cannot deduce. As a consequence, the compiler will exclude that
variable from optimizations and the code will always fetch the variables content
from memory, even if it has done so before (no caching).


This brings up my question: What exactly means "the compiler cannot deduce"?
I'm thinking of a multi-treaded program in which one thread modifies a variable
and another thread reads the variable.

If the whole code is contained in one source file, is it then necessary to make this
variable volatile, or will the compiler be able to deduce that the value of the
variable may be changed in between two read operations in the second thread.

I have the feeling that one should not expect that a compiler takes multi-threading
into account. But I do not know an exact description of "the compiler cannot deduce".

(I know that a call to a function (e.g. to lock/unlock a mutex) is sufficient to tell the compiler
that a variable could be changed, so in practice volatile is not often needed because
multi-threaded programs already use function calls to synchronize their access to
variables.)

F.Z.
Aug 18 '05 #7
Fred Zwarts wrote:
"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message news:43******** *******@gascad. at...
volatile tells the compiler that this variable can get altered by ways that
the compiler cannot deduce. As a consequence, the compiler will exclude that
variable from optimizations and the code will always fetch the variables content
from memory, even if it has done so before (no caching).


This brings up my question: What exactly means "the compiler cannot deduce"?
I'm thinking of a multi-treaded program in which one thread modifies a variable
and another thread reads the variable.

If the whole code is contained in one source file, is it then necessary to make this
variable volatile, or will the compiler be able to deduce that the value of the
variable may be changed in between two read operations in the second thread.

I have the feeling that one should not expect that a compiler takes multi-threading
into account. But I do not know an exact description of "the compiler cannot deduce".

(I know that a call to a function (e.g. to lock/unlock a mutex) is sufficient to tell the compiler
that a variable could be changed, so in practice volatile is not often needed because
multi-threaded programs already use function calls to synchronize their access to
variables.)

F.Z.


A volatile object is one whose value can change asynchronously to the
program's sequence of instructions. For most variables a program can be
sure that the variable's value cannot change except when the program
executes an instruction in its instruction sequence to change it. But
since the value of a volatile variable can change at times that are not
deducible from the program's instruction sequence, the program must
read its value every time that the program uses it. Note that it is
possible to have a const volatile variable. A const volatile variable
would be one whose value can change at any time, but also one whose
value cannot be changed by the program running.

A preemptively multithreaded program should declare any variables
shared between threads as volatile. Each of the program's thread's
sequence of instructions is outside of the instruction sequence
executing on any other thread. A program should also use the "volatile"
keyword on any member functions that are OK to call for a volatile
instance of a class. In fact the "volatile" keyword is pretty much a
parallel of the "const" keyword - for example function parameters can
be declared volatile, just as they can be declared const.

Greg

Aug 18 '05 #8
Fred Zwarts wrote:
"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message news:43******** *******@gascad. at...
volatile tells the compiler that this variable can get altered by ways that
the compiler cannot deduce. As a consequence, the compiler will exclude that
variable from optimizations and the code will always fetch the variables content
from memory, even if it has done so before (no caching).

This brings up my question: What exactly means "the compiler cannot deduce"?
I'm thinking of a multi-treaded program in which one thread modifies a variable
and another thread reads the variable.

If the whole code is contained in one source file, is it then necessary to make this
variable volatile, or will the compiler be able to deduce that the value of the
variable may be changed in between two read operations in the second thread.

I have the feeling that one should not expect that a compiler takes multi-threading
into account. But I do not know an exact description of "the compiler cannot deduce".

(I know that a call to a function (e.g. to lock/unlock a mutex) is sufficient to tell the compiler
that a variable could be changed, so in practice volatile is not often needed because
multi-threaded programs already use function calls to synchronize their access to
variables.)


Volatile has no meaning as far as threaded programs are concerned. So you cannot
infer any useful portable behavior from volatile w.r.t. threading.

Any necessary behavior required to make a threaded program work is covered by
Posix compliance for that compiler and only covers pthread functions. Volatile
is not affected by Posix compliance. It's still meaningless. It has no effect
on the correctness of threaded programs. If you think volatile is required to
make your threaded program correct then you most certainly have an error in your
program logic.

--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Aug 18 '05 #9

Joe Seigh wrote:
Fred Zwarts wrote:
"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message news:43******** *******@gascad. at...
volatile tells the compiler that this variable can get altered by ways that
the compiler cannot deduce. As a consequence, the compiler will exclude that
variable from optimizations and the code will always fetch the variables content
from memory, even if it has done so before (no caching).

This brings up my question: What exactly means "the compiler cannot deduce"?
I'm thinking of a multi-treaded program in which one thread modifies a variable
and another thread reads the variable.

If the whole code is contained in one source file, is it then necessary to make this
variable volatile, or will the compiler be able to deduce that the value of the
variable may be changed in between two read operations in the second thread.

I have the feeling that one should not expect that a compiler takes multi-threading
into account. But I do not know an exact description of "the compiler cannot deduce".

(I know that a call to a function (e.g. to lock/unlock a mutex) is sufficient to tell the compiler
that a variable could be changed, so in practice volatile is not often needed because
multi-threaded programs already use function calls to synchronize their access to
variables.)


Volatile has no meaning as far as threaded programs are concerned. So you cannot
infer any useful portable behavior from volatile w.r.t. threading.

Any necessary behavior required to make a threaded program work is covered by
Posix compliance for that compiler and only covers pthread functions. Volatile
is not affected by Posix compliance. It's still meaningless. It has no effect
on the correctness of threaded programs. If you think volatile is required to
make your threaded program correct then you most certainly have an error in your
program logic.

--
Joe Seigh


The volatile keyword has the same meaning in any C++ program. The
volatile keyword does not state how the value can change asynchronously
- be it by an interrupt, a signal, memory-mapped I/O, or by another
thread running in the same process.

Since C++ has no concept of multiple threads, it has no way of
coordinating instruction sequences between threads. Therefore one
thread changing a shared variable is to the other threads an
asynchronous change. Such a variable should therefore be declared
volatile to prevent the program running on any thread from caching its
value.

There is certainly plenty of literature describing the importance of
the volatile keyword to multithreaded programming. Here is one such
article:

http://www.cuj.com/documents/s=7998/...p1902alexandr/

Greg

Aug 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3206
by: r_o_h_i_t | last post by:
Can anybody please tell me the complete properties if volatile variables in C. And what are specific uses of them? Thanks in advance.
14
19266
by: google-newsgroups | last post by:
Hello, even (or because?) reading the standard (ISO/IEC 9899/1999) I do not understand some issues with volatile. The background is embedded programming where data is exchanged between main program flow and interrupts. At the organisation I work, instead of declaring a variable volatile, it is casted to volatile when necessary:
22
2264
by: Assaf | last post by:
hi all i know that i should not cross-post, but i am not sure to which group to post this question. 2 quesions about volatile: 1. i use volatile when 2 threads access the same variable is this the proper use of volatile?
6
3214
by: Clausfor | last post by:
Hello, I have a problem with restoring variables in the setjmp/longjmp functions: K&R2 for longjmp says: "Accessible objects have the same value they had when longjmp was called, except for automatic non volatile variables of the function invoking setjmp, these will be undefined if modified after the setjmp call"
6
3525
by: titan nyquist | last post by:
Can you make volatile structures in C#? I have a static class, to have "global" variables. This allows the whole program to see them. I make them "volatile" to avoid multi- threading accessing issues. That works. THE PROBLEM: In that static class, I want to combine some variables inside a structure, and then make a variable of that...
94
30225
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
10
3417
by: S James S Stapleton | last post by:
Is volatile necessary anymore? I have a two-thread piece of code I've been testing to figure out what volatile does (fairly simple code, uses pthreads). I have an update thread (variables passed as volatile) and a print thread (one variable volatile, the other, not). There is no difference in the behavior of the volatile and nonvolatile...
16
1810
by: Ark Khasin | last post by:
I have a memory-mapped peripheral with a mapping like, say, struct T {uint8_t read, write, status, forkicks;}; If I slap a volatile on an object of type struct T, does it guarantee that all accesses to the members are byte-wide, or is the compiler free to read or read-modify-write in any data width it chooses? Is slapping a volatile on...
3
2316
by: C++Liliput | last post by:
It seems that the keyword "volatile" is used to make sure that threads reading (or writing to) the same data should see a consistent picture of the variable i.e. updates made to the common data should immediately get reflected to other threads that are using it. However I have never faced this issue in y multi-threaded code even though the...
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.