Solved

data type use

Posted on 2000-03-16
2
243 Views
Last Modified: 2010-04-02
how do i make use of the in-built data type volatile?

i do not know how it can be put to practical use.
thanx in advance.
0
Comment
Question by:vjkrm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
CJ_S earned 50 total points
ID: 2623312
I hardly ever use volatile unless I need multithreading or when some other programs needs to change variables.

See, when I am making a multithreading program I need all the threads to be able to work with variables, so this will be volatile.

Small example

int volatile Number

Mutlithread1: changes this value to 2

Multithread2: reads the value, and sees 2


if you don't use volatile the integer will be 'locked'

Volatile is also usefull for memory places, so other programs can use these values.

I hope that did help you some
0
 
LVL 22

Expert Comment

by:nietod
ID: 2623526
You use volatile on global or static variables that may be used by more than 1 thread.  It does not make sense to use on local variables, because these cannot be used by more than 1 thread.  (actually, you could construct cases where that would not be true, but that would be very very unusual, so you can use it on locals, but I really doubt it woudl ever be needed.)  It insures that the compiler does not optimize the code in some way so that changes made to the variable by one thread are not detected by other threads.  (basically it insures that the compiler does not "cache" the variable in a register even under conditions where it seems safe to do so.)

By itself "volatile" is usually not enough to insure that a variable may be safely accessed by multiple threads.  You will often also need a synchronization mechanism, like a mutex, to insure that only one thread access the variable at a time.  (The mutex insures that one thread uses the variable at a time and the "volatile" insures that the thread really accesses it, rather than a cached copy.)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question