Solved

data type use

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

803 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