volatile variables

J

junky_fellow

Guys,

If I have a global variable, that is shared across multiple
threads on an SMP environment, then do I need to declare it as
volatile, so that a particular processor does not store and access its
vlaue fron the processor registers ?

thnaks in advance for any help ...
 
H

Hari

(e-mail address removed) je napisao:
Guys,

If I have a global variable, that is shared across multiple
threads on an SMP environment, then do I need to declare it as
volatile, so that a particular processor does not store and access its
vlaue fron the processor registers ?

thnaks in advance for any help ...

Having volatile type qualifier is mainly to do with the problems that
are encountered in real-time or embedded systems.
so that a particular processor does not store and access its
vlaue fron the processor registers

yes, volatile will use direct access, and probably switch of
optimization
If I have a global variable, that is shared across multiple
threads on an SMP environment, then do I need to declare it as
volatile

I think that you must use some thread locking mechanism, not volatile,
to use global variable with multiple threads. Search net for thread
locking
for your platform.

Best,
Zaharije Pasalic
 
M

Mohan

Guys,

If I have a global variable, that is shared across multiple
threads on an SMP environment, then do I need to declare it as
volatile, so that a particular processor does not store and access its
vlaue fron the processor registers ?

Yes, it is required to be a volatile.

Mohan
 
E

Eric Sosman

Guys,

If I have a global variable, that is shared across multiple
threads on an SMP environment, then do I need to declare it as
volatile, so that a particular processor does not store and access its
vlaue fron the processor registers ?

Strange though it seems, this isn't a question about C.
Yes, you're asking about a C keyword and its effect on the
way a C program executes, but it's still not a C question.

The reason is that C's view of the way a program executes
is almost entirely single-threaded. There are a few hints --
volatile, signals -- that other agencies than "the" program
might be at work, but this part of the picture is blurry and
indistinct. C describes the effects of function calls, for
loops, if statements, and so on in terms of a single thread
of execution. The possibility of other threads isn't ruled
out altogether, but there's not enough information about how
they might work to be able to infer much about how they might
interact with each other.

... and in fact, different systems have different ways of
supporting asynchronous activities, leading to different ways
in which separate activities interact and coordinate. They
may start with C, but they build upon it in different -- and
not entirely compatible -- ways. And your question, phrased
in terms of a C keyword, is really about those beyond-C matters.

<off-topic>

To learn more about the "thread" model of asynchronous
execution, you may want to read comp.programming.threads for
a while. Among other things, you will learn that the volatile
keyword is neither necessary nor sufficient for sharing data
between multiple threads of execution; it's just irrelevant.

</off-topic>
 
J

Jean-Marc Bourguet

If I have a global variable, that is shared across multiple
threads on an SMP environment, then do I need to declare it as
volatile, so that a particular processor does not store and access its
vlaue fron the processor registers ?

volatile may work for that or not. It is not the usage for which it has
been designed (memory mapped IO) and common implementations don't do all
what is needed for sharing data across threads (not only do a real access
but also have memory barrier instructions; for memory mapped IO these
instructions are not needed as in this case the memory region is marked
non-cachable by the OS).

Yours,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top