Solved

data type use

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

759 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

20 Experts available now in Live!

Get 1:1 Help Now