Inferno Web Development
  • Home
  • Forums
  • About
  • Links
  • Contact

Tutorials List

  • C++ Tutorials

Articles

  • Java
  • PHP
  • Photoshop
  • C#
  • C++ Qt GUI
  • C++ Win32 API
  • C++
  • MASM32
  • General News
  • JavaScript
  • Web Development
  • Windows Tweaks

Popular Tutorials

All time:

  • Website Header with 3D Fold Up / Lift Effect
  • C++ Win32 API Tutorial
  • Perfect C++ String Explode Split
  • Simple C++ DLL Loading a Message Box
  • Simple C++ Pointers and References

Related Articles

  • Perfect C++ String Explode Split
  • C/C++ Basic Structures of a Simple Program
  • Simple C++ Pointers and References
  • Inheritance, Polymorphism, and Virtual Functions
  • Advanced Classes, Constructors, Destructors, and Copy Constructors

Programming Tutorials

Home

C++ Volatile Keyword

Submitted by Baran Ornarli on Mon, 11/03/2008 - 06:18.
  • C++

Volatile keyword can be specified for any C++ variable in order to tell the compiler that the variable should not be optimized. I'll show you what this means and why this Computer Engineering secret is very rare to find in any code. It's used in multi-threaded code or embedded system designs.

Take this code for example:

volatile int importantCheck;
 
void InterruptOrThread(){
  importantCheck = 5;
}
 
void main(){
  importantCheck = 0;
  CreateThread();
  ExecuteActions();
  DoEvents();
  if(importantCheck == 5){
    LaunchProgram();
  }
}

If we did not have the volatile keyword on importantCheck integer, then the compiler will optimize this code, because the InterruptOrThread() function isn't called directly by the main thread, but it is called as a thread or an interrupt in an embedded system.

Volatile means that the C++ variable should not be optimized.

The compiler will remove the check "importantCheck == 5" and it will remove the LaunchProgram() function, because the compiler believes that it's a mistake and that importantCheck will never be 5, so it removes it.

Volatile is used in multi-threaded or interrupt driven programs because the compiler shouldn't optimize all variables because compilers cannot understand threads very well.


5
Average: 5 (1 vote)
»
  • Share Tutorial

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <img> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h2>
  • Lines and paragraphs break automatically.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • Table of contents based on the <h*> tags

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
  ooooooooo   .o88o.               oooooo   oooo  ooooooooooooo  oooo        
d"""""""8' 888 `" `888. .8' 8' 888 `8 `888
.8' o888oo oooo ooo `888. .8' 888 888 oooo
.8' 888 `88. .8' `888.8' 888 888 .8P'
.8' 888 `88..8' `888' 888 888888.
.8' 888 `888' 888 888 888 `88b.
.8' o888o `8' o888o o888o o888o o888o


Enter the code depicted in ASCII art style.

Navigation

  • Home
  • Forums
  • Image Gallery
  • Links
  • About
  • Contact

Why Register? Contribute articles and tutorials to ID, earn titles, learn from ID programming projects, and advertise your blog in our forums.

  • Forum Community
  • Register Now
  • Write an Article


Copyright © Inferno Development 2008. All Rights Reserved.