Talk about C++ mutable and volatile
- 2020-11-20 06:10:43
- OfStack
There are three key words in C++ that modify variable data:
const
,
volatile
and
mutable
.
const
It's easy to understand, meaning that the content it modifies is immutable (at least at compile time), while
volatile
and
mutable
On the contrary, the indicator data is always variable.
mutable
and
volatile
All you can and
const
Collocation is used, but there is a big difference in the use of both.
mutable
mutable
Applies only to class members, indicating that their data is always variable. Can't and
const
In a method decorated with const, the member data of mutable can be modified without causing any side effects to the class/object.
Consider a
mutable
In order to protect the privacy of the driver, the contact number displayed by the driver will be updated from the free number pool every 5 minutes. According to the requirements, the implementation of Driver class is as follows:
class Driver {
private:
...
// real phone number
string phone;
// display phone number
mutable string displayPhone;
public:
string getDisplayPhone() const {
if (needUpdate()) {
lock.lock();
if (needUpdate()) {
updateDisplayPhone(); // displayPhone It's changed here
}
lock.unlock();
}
return displayPhone;
}
};
In the above code,
const
Changes to regular members are not allowed in the method, but
mutable
Members are not subject to this restriction. right
Driver
In terms of class, its inherent attributes (name, age, real mobile phone number, etc.) have not changed and are consistent
const
Modification.
mutable
Let 1 at any time variable display properties can be changed, to achieve the purpose of flexible programming.
volatile
volatile
Used to modify a member or variable to indicate that the object it modifies may change at any time. The compiler should not optimize the variable it modifies (caching). Each value should be read directly from memory. Due to the
volatile
The changes come from the run-time, which can be compared with
const
1. Start using. Using both can be confusing at first, but it's much easier if you consider the scenario: CPU and GPU map the same block in common memory, and GPU can write data into Shared memory at any time. For programs on CPU,
const
The modifier variable 1 is straight to the right, so the compilation passes. But the values in the memory of its variables can change at any time during the run,
volatile
Embellishment is the right thing to do.
In a multithreaded environment,
volatile
Can be used as a means of memory synchronization. For example, multithreaded blasting password:
volatile bool found = false;
void run(string target) {
while (!found) {
// Calculate the hash of a dictionary password
if (target == hash) {
found = true;
break;
}
}
}
in
volatile
Each loop checks the value in memory to achieve synchronization.
The thing to notice is,
volatile
Is subject to change at any time, which can lead to unexpected results. Take the following example to sum the squares:
double square(volatile double a, volatile double b) {
return (a + b) * (a + b);
}
Both a and b are mutable, so the first a + b in the above code could be different from the second, leading to unexpected results. In this case, the correct approach is to assign values to regular variables and then multiply them:
double square(volatile double a, volatile double b) {
double c = a + b;
return c * c;
}
conclusion
mutable
Can only be used with class variables, not with
const
Use simultaneously; in
const
In the modification method,
mutable
The value of a variable can change;
volatile
It is just that the value of a run-time variable can change from time to time, either from another thread or from an external system.
These are the details of mutable and volatile to talk about C++, for more information about C++ mutable and volatile, please pay attention to other related articles on this site!