How to Use the Volatile Keyword in C++? Last Updated : 19 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, the volatile keyword is used to tell the compiler that the value of the variable declared using volatile may change at any time. In this article, we will learn how to use the volatile keyword in C++. Volatile Keyword in C++We can use the volatile keyword for different purposes like declaring some global variables, variables across shared threads, etc. Syntax to use Volatile Keyword in C++volatile dataType varName;C++ Program to Show the Use of Volatile Keyword C++ // C++ Program to Show how to use the Volatile Keyword #include <iostream> #include <mutex> #include <thread> using namespace std; // Mutex for synchronization mutex mtx; // Volatile variable to be accessed by multiple threads volatile int volVar = 0; // Function to increment the volatile variable void incValue() { for (int i = 0; i < 10; i++) { mtx.lock(); // Lock the mutex before accessing // volVar volVar++; mtx.unlock(); // Unlock the mutex after modifying // volVar } } int main() { // Create two threads to increment volVar thread t1(incValue); thread t2(incValue); // Wait for both threads to finish t1.join(); t2.join(); // Output the final value of volVar cout << "Final value of volVar: " << volVar << endl; return 0; } Output Final value of volVar: 20 Time Complexity: O(1), for each lock and unlock operation.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article volatile Qualifier in C++ P pradeep1210 Follow Improve Article Tags : C++ Programs C++ CPP Examples Practice Tags : CPP Similar Reads How to Use Const Keyword in C++? In C++, the const keyword is used to declare constants or unchangeable values. It indicates an immutable object that cannot be modified. The const keyword can be applied to variables, pointers, member functions, objects, and references. In this article, we will learn how to use the const keyword in 4 min read How to use const with Pointers in C++? In C++, the const keyword is used as a type qualifier for defining read-only (immutable) objects that cannot be modified anywhere in their lifetime. It can be used in several ways with pointers, each serving a different purpose. In this article, we will learn how to use const qualifier with pointers 3 min read C++ Program to Show Use of This Keyword in Class Here, we will see how to use this keyword in a class using a C++ program. this keyword in C++ is an implicit pointer that points to the object of the class of which the member function is called. Every object has its own this pointer. Every object can reference itself by this pointer. There are 4 wa 4 min read volatile Qualifier in C++ volatile keyword in C++ is used to declare variables that can be modified by external factors outside the program's control. This qualifier informs the compiler that the variable's value may change anytime, preventing certain optimizations that could lead to unexpected behavior. In this article, we 8 min read How to Use the std::mutex Synchronization Primitive in C++ In multi-threaded programming, it is essential to ensure that shared resources are accessed in a controlled and synchronized manner to maintain data consistency and prevent race conditions. The std::mutex synchronization primitive was introduced in C++ 11 to allow threads to acquire exclusive owners 3 min read How to Implement User Defined Shared Pointers in C++? shared_ptr is one of the smart pointer introduced as a wrapper to the old raw pointers in C++ to help in avoiding the risks and errors of raw pointers. In this article, we will learn how to implement our own user defined shared pointer in C++. What is shared_ptr in C++? A std::shared_ptr is a contai 5 min read Like