Solved

data type use

Posted on 2000-03-16
2
207 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now