C++中volatile和mutable关键字用法详解

 更新时间:2020年02月01日 14:03:05   作者:fengbingchun  
这篇文章主要介绍了C++中volatile和mutable关键字用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

C/C++中的volatile关键字和const对应,用来修饰变量,用于告诉编译器该变量值是不稳定的,可能被更改。使用volatile注意事项:

(1). 编译器会对带有volatile关键字的变量禁用优化(A volatile specifier is a hint to a compiler that an object may change its value in ways not specified by the language so that aggressive optimizations must be avoided)。

(2). 当多个线程都要用到某一个变量且该变量的值会被改变时应该用volatile声明,该关键字的作用是防止编译器优化把变量从内存装入CPU寄存器中。如果变量被装入寄存器,那么多个线程有可能有的使用内存中的变量,有的使用寄存器中的变量,这会造成程序的错误执行。volatile的意思是让编译器每次操作该变量时一定要从内存中取出,而不是使用已经存在寄存器中的值(It cannot cache the variables in register)。

(3). 中断服务程序中访问到的变量最好带上volatile。

(4). 并行设备的硬件寄存器的变量最好带上volatile。

(5). 声明的变量可以同时带有const和volatile关键字。

(6). 多个volatile变量间的操作,是不会被编译器交换顺序的,能够保证volatile变量间的顺序性,编译器不会进行乱序优化(The value cannot change in order of assignment)。但volatile变量和非volatile变量之间的顺序,编译器不保证顺序,可能会进行乱序优化。

C++中的mutable关键字使用场景

(1). 允许即使包含它的对象被声明为const时仍可修改声明为mutable的类成员(sometimes there is requirement to modify one or more data members of class/struct through const function even though you don't want the function to update other members of class/struct. This task can be easily performed by using mutable keyword)。

(2). 应用在C++11 lambda表达式来表示按值捕获的值是可修改的,默认情况下是不可修改的,但修改仅在lambda式内有效(since c++11 mutable can be used on a lambda to denote that things captured by value are modifiable (they aren't by default))。

详细用法见下面的测试代码,下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "volatile_mutable.hpp"
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <mutex>
#include <string.h>
 
namespace volatile_mutable_ {
 
///////////////////////////////////////////////////////////
int test_volatile_1()
{
 volatile int i1 = 0; // correct
 int volatile i2 = 0; // correct
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://en.cppreference.com/w/c/language/volatile
int test_volatile_2()
{
{ // Any attempt to read or write to an object whose type is volatile-qualified through a non-volatile lvalue results in undefined behavior
 volatile int n = 1; // object of volatile-qualified type
 int* p = (int*)&n;
 int val = *p; // undefined behavior in C, Note: link does not report an error under C++
 fprintf(stdout, "val: %d\n", val);
}
 
{ // A member of a volatile-qualified structure or union type acquires the qualification of the type it belongs to
 typedef struct ss { int i; const int ci; } s;
 // the type of s.i is int, the type of s.ci is const int
 volatile s vs = { 1, 2 };
 // the types of vs.i and vs.ci are volatile int and const volatile int
}
 
{ // If an array type is declared with the volatile type qualifier (through the use of typedef), the array type is not volatile-qualified, but its element type is
 typedef int A[2][3];
 volatile A a = { {4, 5, 6}, {7, 8, 9} }; // array of array of volatile int
 //int* pi = a[0]; // Error: a[0] has type volatile int*
 volatile int* pi = a[0];
}
 
{ // A pointer to a non-volatile type can be implicitly converted to a pointer to the volatile-qualified version of the same or compatible type. The reverse conversion can be performed with a cast expression
 int* p = nullptr;
 volatile int* vp = p; // OK: adds qualifiers (int to volatile int)
 //p = vp; // Error: discards qualifiers (volatile int to int)
 p = (int*)vp; // OK: cast
}
 
{ // volatile disable optimizations
 clock_t t = clock();
 double d = 0.0;
 for (int n = 0; n < 10000; ++n)
 for (int m = 0; m < 10000; ++m)
  d += d * n*m; // reads and writes to a non-volatile 
 fprintf(stdout, "Modified a non-volatile variable 100m times. Time used: %.2f seconds\n", (double)(clock() - t) / CLOCKS_PER_SEC);
 
 t = clock();
 volatile double vd = 0.0;
 for (int n = 0; n < 10000; ++n)
 for (int m = 0; m < 10000; ++m)
  vd += vd * n*m; // reads and writes to a volatile 
 fprintf(stdout, "Modified a volatile variable 100m times. Time used: %.2f seconds\n", (double)(clock() - t) / CLOCKS_PER_SEC);
}
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://en.cppreference.com/w/cpp/language/cv
int test_volatile_3()
{
 int n1 = 0;      // non-const object
 const int n2 = 0;   // const object
 int const n3 = 0;   // const object (same as n2)
 volatile int n4 = 0; // volatile object
 const struct {
 int n1;
 mutable int n2;
 } x = { 0, 0 };   // const object with mutable member
 
 n1 = 1; // ok, modifiable object
 //n2 = 2; // error: non-modifiable object
 n4 = 3; // ok, treated as a side-effect
 //x.n1 = 4; // error: member of a const object is const
 x.n2 = 4; // ok, mutable member of a const object isn't const
 
 const int& r1 = n1; // reference to const bound to non-const object
 //r1 = 2; // error: attempt to modify through reference to const
 const_cast<int&>(r1) = 2; // ok, modifies non-const object n1
 fprintf(stdout, "n1: %d\n", n1); // 2
 
 const int& r2 = n2; // reference to const bound to const object
 //r2 = 2; // error: attempt to modify through reference to const
 const_cast<int&>(r2) = 2; // undefined behavior: attempt to modify const object n2, Note: link does not report an error under C++
 fprintf(stdout, "n2: %d\n", n2); // 0
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://www.geeksforgeeks.org/understanding-volatile-qualifier-in-c/
int test_volatile_4()
{
{
 const int local = 10;
 int *ptr = (int*)&local;
 fprintf(stdout, "Initial value of local : %d \n", local); // 10
 
 *ptr = 100;
 fprintf(stdout, "Modified value of local: %d \n", local); // 10
}
 
{
 const volatile int local = 10;
 int *ptr = (int*)&local;
 fprintf(stdout, "Initial value of local : %d \n", local); // 10
 
 *ptr = 100;
 fprintf(stdout, "Modified value of local: %d \n", local); // 100
}
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://en.cppreference.com/w/cpp/language/cv
int test_mutable_1()
{
 // Mutable is used to specify that the member does not affect the externally visible state of the class (as often used for mutexes,
 // memo caches, lazy evaluation, and access instrumentation)
 class ThreadsafeCounter {
 public:
 int get() const {
  std::lock_guard<std::mutex> lk(m);
  return data;
 }
 void inc() {
  std::lock_guard<std::mutex> lk(m);
  ++data;
 }
 
 private:
 mutable std::mutex m; // The "M&M rule": mutable and mutex go together
 int data = 0;
 };
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://www.tutorialspoint.com/cplusplus-mutable-keyword
int test_mutable_2()
{
 class Test {
 public:
 Test(int x = 0, int y = 0) : a(x), b(y) {}
 
 void seta(int x = 0) { a = x; }
 void setb(int y = 0) { b = y; }
 void disp() { fprintf(stdout, "a: %d, b: %d\n", a, b); }
 
 public:
 int a;
 mutable int b;
 };
 
 const Test t(10, 20);
 fprintf(stdout, "t.a: %d, t.b: %d \n", t.a, t.b); // 10, 20
 
 //t.a=30; // Error occurs because a can not be changed, because object is constant.
 t.b = 100; // b still can be changed, because b is mutable.
 fprintf(stdout, "t.a: %d, t.b: %d \n", t.a, t.b); // 10, 100
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://www.geeksforgeeks.org/c-mutable-keyword/
int test_mutable_3()
{
 using std::cout;
 using std::endl;
 
 class Customer {
 public:
 Customer(char* s, char* m, int a, int p)
 {
  strcpy(name, s);
  strcpy(placedorder, m);
  tableno = a;
  bill = p;
 }
 
 void changePlacedOrder(char* p) const { strcpy(placedorder, p); }
 void changeBill(int s) const { bill = s; }
 
 void display() const
 {
  cout << "Customer name is: " << name << endl;
  cout << "Food ordered by customer is: " << placedorder << endl;
  cout << "table no is: " << tableno << endl;
  cout << "Total payable amount: " << bill << endl;
 }
 
 private:
 char name[25];
 mutable char placedorder[50];
 int tableno;
 mutable int bill;
 };
 
 const Customer c1("Pravasi Meet", "Ice Cream", 3, 100);
 c1.display();
 c1.changePlacedOrder("GulabJammuns");
 c1.changeBill(150);
 c1.display();
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://stackoverflow.com/questions/105014/does-the-mutable-keyword-have-any-purpose-other-than-allowing-the-variable-to
int test_mutable_4()
{
 int x = 0;
 auto f1 = [=]() mutable { x = 42; }; // OK
 //auto f2 = [=]() { x = 42; }; // Error: a by-value capture cannot be modified in a non-mutable lambda
 fprintf(stdout, "x: %d\n", x); // 0
 
 return 0;
}
 
} // namespace volatile_mutable_

GitHub:https://github.com/fengbingchun/Messy_Test

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解C++中的内存同步模式(memory order)

    详解C++中的内存同步模式(memory order)

    这篇文章主要介绍了C++中的内存同步模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • C语言利用EasyX绘制小企鹅表情包

    C语言利用EasyX绘制小企鹅表情包

    这篇文章主要为大家详细介绍了C语言如何利用EasyX绘图库实现绘制可爱的小企鹅表情包,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-12-12
  • C语言字符串快速压缩算法代码

    C语言字符串快速压缩算法代码

    这篇文章主要介绍了C语言字符串快速压缩算法代码,将字符串中连续出席的重复字母进行压缩,其主要的压缩字段的格式为”字符重复的次数+字符”。有需要的小伙伴参考下吧。
    2015-03-03
  • 一些语言的按行读取文件的代码实现小结

    一些语言的按行读取文件的代码实现小结

    这篇文章主要介绍了一些语言的按行读取文件的代码实现小结,这里罗列了Java和C语言和C++以及PHP的实现需要的朋友可以参考下
    2015-08-08
  • 原码, 反码与补码基础知识详细介绍

    原码, 反码与补码基础知识详细介绍

    这篇文章讲解了计算机的原码, 反码和补码. 并且进行了深入探求了为何要使用反码和补码, 以及更进一步的论证了为何可以用反码, 补码的加法计算原码的减法,需要的朋友可以参考下
    2016-12-12
  • C/C++ Qt ToolBar菜单组件的具体使用

    C/C++ Qt ToolBar菜单组件的具体使用

    ToolBar工具栏在所有窗体应用程序中都广泛被使用,使用ToolBar可以很好的规范菜单功能分类,本文就详细的介绍一下ToolBar组件的应用,感兴趣的可以了解一下
    2021-11-11
  • C++初始化函数列表详细解析

    C++初始化函数列表详细解析

    C++可以定义引用类型的成员变量,引用类型的成员变量必须在构造函数的初始化列表中进行初始化
    2013-09-09
  • C语言实现哈夫曼树的构建

    C语言实现哈夫曼树的构建

    这篇文章主要为大家详细介绍了C语言实现哈夫曼树的构建,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • C++执行shell命令的多种实现方法

    C++执行shell命令的多种实现方法

    在linux系统下,用C++程序执行shell命令有多种方式,主要介绍了3中方法,具有一定的参考价值,感兴趣的可以了解一下
    2021-11-11
  • linux之awk命令的用法

    linux之awk命令的用法

    awk是一个非常棒的数字处理工具。相比于sed常常作用于一整行的处理,awk则比较倾向于将一行分为数个“字段”来处理。运行效率高,而且代码简单,对格式化的文本处理能力超强
    2013-10-10

最新评论