?
Solved

data type use

Posted on 2000-03-16
2
Medium Priority
?
249 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 100 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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