Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > volatile variables

Reply
Thread Tools

volatile variables

 
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      05-14-2007
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 ...

 
Reply With Quote
 
 
 
 
Hari
Guest
Posts: n/a
 
      05-14-2007

http://www.velocityreviews.com/forums/(E-Mail 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

 
Reply With Quote
 
 
 
 
Mohan
Guest
Posts: n/a
 
      05-14-2007

(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-14-2007
(E-Mail Removed) wrote:
> 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>

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Jean-Marc Bourguet
Guest
Posts: n/a
 
      05-14-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> 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,

--
Jean-Marc
 
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
longjmp and volatile variables jacob navia C Programming 6 05-01-2006 05:25 PM
Atomic operation and volatile variables Sumukh Java 8 09-23-2005 10:42 PM
Use of the Volatile keyword for a pointer to a volatile memory block ben C Programming 5 01-11-2005 05:38 PM
Volatile variables srinivas reddy C Programming 11 03-01-2004 05:46 PM
Volatile variables in C!! r_o_h_i_t C Programming 7 02-17-2004 11:43 AM



Advertisments