飞道的博客

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

962人阅读  评论(0)

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:


  
  1. #include "volatile_mutable.hpp"
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <mutex>
  6. #include <string.h>
  7. namespace volatile_mutable_ {
  8. ///////////////////////////////////////////////////////////
  9. int test_volatile_1()
  10. {
  11. volatile int i1 = 0; // correct
  12. int volatile i2 = 0; // correct
  13. return 0;
  14. }
  15. ///////////////////////////////////////////////////////////
  16. // reference: https://en.cppreference.com/w/c/language/volatile
  17. int test_volatile_2()
  18. {
  19. { // Any attempt to read or write to an object whose type is volatile-qualified through a non-volatile lvalue results in undefined behavior
  20. volatile int n = 1; // object of volatile-qualified type
  21. int* p = ( int*)&n;
  22. int val = *p; // undefined behavior in C, Note: link does not report an error under C++
  23. fprintf( stdout, "val: %d\n", val);
  24. }
  25. { // A member of a volatile-qualified structure or union type acquires the qualification of the type it belongs to
  26. typedef struct ss { int i; const int ci; } s;
  27. // the type of s.i is int, the type of s.ci is const int
  28. volatile s vs = { 1, 2 };
  29. // the types of vs.i and vs.ci are volatile int and const volatile int
  30. }
  31. { // 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
  32. typedef int A[ 2][ 3];
  33. volatile A a = { { 4, 5, 6}, { 7, 8, 9} }; // array of array of volatile int
  34. //int* pi = a[0]; // Error: a[0] has type volatile int*
  35. volatile int* pi = a[ 0];
  36. }
  37. { // 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
  38. int* p = nullptr;
  39. volatile int* vp = p; // OK: adds qualifiers (int to volatile int)
  40. //p = vp; // Error: discards qualifiers (volatile int to int)
  41. p = ( int*)vp; // OK: cast
  42. }
  43. { // volatile disable optimizations
  44. clock_t t = clock();
  45. double d = 0.0;
  46. for ( int n = 0; n < 10000; ++n)
  47. for ( int m = 0; m < 10000; ++m)
  48. d += d * n*m; // reads and writes to a non-volatile
  49. fprintf( stdout, "Modified a non-volatile variable 100m times. Time used: %.2f seconds\n", ( double)(clock() - t) / CLOCKS_PER_SEC);
  50. t = clock();
  51. volatile double vd = 0.0;
  52. for ( int n = 0; n < 10000; ++n)
  53. for ( int m = 0; m < 10000; ++m)
  54. vd += vd * n*m; // reads and writes to a volatile
  55. fprintf( stdout, "Modified a volatile variable 100m times. Time used: %.2f seconds\n", ( double)(clock() - t) / CLOCKS_PER_SEC);
  56. }
  57. return 0;
  58. }
  59. ///////////////////////////////////////////////////////////
  60. // reference: https://en.cppreference.com/w/cpp/language/cv
  61. int test_volatile_3()
  62. {
  63. int n1 = 0; // non-const object
  64. const int n2 = 0; // const object
  65. int const n3 = 0; // const object (same as n2)
  66. volatile int n4 = 0; // volatile object
  67. const struct {
  68. int n1;
  69. mutable int n2;
  70. } x = { 0, 0 }; // const object with mutable member
  71. n1 = 1; // ok, modifiable object
  72. //n2 = 2; // error: non-modifiable object
  73. n4 = 3; // ok, treated as a side-effect
  74. //x.n1 = 4; // error: member of a const object is const
  75. x.n2 = 4; // ok, mutable member of a const object isn't const
  76. const int& r1 = n1; // reference to const bound to non-const object
  77. //r1 = 2; // error: attempt to modify through reference to const
  78. const_cast< int&>(r1) = 2; // ok, modifies non-const object n1
  79. fprintf( stdout, "n1: %d\n", n1); // 2
  80. const int& r2 = n2; // reference to const bound to const object
  81. //r2 = 2; // error: attempt to modify through reference to const
  82. const_cast< int&>(r2) = 2; // undefined behavior: attempt to modify const object n2, Note: link does not report an error under C++
  83. fprintf( stdout, "n2: %d\n", n2); // 0
  84. return 0;
  85. }
  86. ///////////////////////////////////////////////////////////
  87. // reference: https://www.geeksforgeeks.org/understanding-volatile-qualifier-in-c/
  88. int test_volatile_4()
  89. {
  90. {
  91. const int local = 10;
  92. int *ptr = ( int*)&local;
  93. fprintf( stdout, "Initial value of local : %d \n", local); // 10
  94. *ptr = 100;
  95. fprintf( stdout, "Modified value of local: %d \n", local); // 10
  96. }
  97. {
  98. const volatile int local = 10;
  99. int *ptr = ( int*)&local;
  100. fprintf( stdout, "Initial value of local : %d \n", local); // 10
  101. *ptr = 100;
  102. fprintf( stdout, "Modified value of local: %d \n", local); // 100
  103. }
  104. return 0;
  105. }
  106. ///////////////////////////////////////////////////////////
  107. // reference: https://en.cppreference.com/w/cpp/language/cv
  108. int test_mutable_1()
  109. {
  110. // Mutable is used to specify that the member does not affect the externally visible state of the class (as often used for mutexes,
  111. // memo caches, lazy evaluation, and access instrumentation)
  112. class ThreadsafeCounter {
  113. public:
  114. int get() const {
  115. std::lock_guard< std::mutex> lk(m);
  116. return data;
  117. }
  118. void inc() {
  119. std::lock_guard< std::mutex> lk(m);
  120. ++data;
  121. }
  122. private:
  123. mutable std::mutex m; // The "M&M rule": mutable and mutex go together
  124. int data = 0;
  125. };
  126. return 0;
  127. }
  128. ///////////////////////////////////////////////////////////
  129. // reference: https://www.tutorialspoint.com/cplusplus-mutable-keyword
  130. int test_mutable_2()
  131. {
  132. class Test {
  133. public:
  134. Test( int x = 0, int y = 0) : a(x), b(y) {}
  135. void seta(int x = 0) { a = x; }
  136. void setb(int y = 0) { b = y; }
  137. void disp() { fprintf( stdout, "a: %d, b: %d\n", a, b); }
  138. public:
  139. int a;
  140. mutable int b;
  141. };
  142. const Test t(10, 20);
  143. fprintf( stdout, "t.a: %d, t.b: %d \n", t.a, t.b); // 10, 20
  144. //t.a=30; // Error occurs because a can not be changed, because object is constant.
  145. t.b = 100; // b still can be changed, because b is mutable.
  146. fprintf( stdout, "t.a: %d, t.b: %d \n", t.a, t.b); // 10, 100
  147. return 0;
  148. }
  149. ///////////////////////////////////////////////////////////
  150. // reference: https://www.geeksforgeeks.org/c-mutable-keyword/
  151. int test_mutable_3()
  152. {
  153. using std:: cout;
  154. using std:: endl;
  155. class Customer {
  156. public:
  157. Customer( char* s, char* m, int a, int p)
  158. {
  159. strcpy(name, s);
  160. strcpy(placedorder, m);
  161. tableno = a;
  162. bill = p;
  163. }
  164. void changePlacedOrder(char* p) const { strcpy(placedorder, p); }
  165. void changeBill(int s) const { bill = s; }
  166. void display() const
  167. {
  168. cout << "Customer name is: " << name << endl;
  169. cout << "Food ordered by customer is: " << placedorder << endl;
  170. cout << "table no is: " << tableno << endl;
  171. cout << "Total payable amount: " << bill << endl;
  172. }
  173. private:
  174. char name[ 25];
  175. mutable char placedorder[ 50];
  176. int tableno;
  177. mutable int bill;
  178. };
  179. const Customer c1("Pravasi Meet", "Ice Cream", 3, 100);
  180. c1.display();
  181. c1.changePlacedOrder( "GulabJammuns");
  182. c1.changeBill( 150);
  183. c1.display();
  184. return 0;
  185. }
  186. ///////////////////////////////////////////////////////////
  187. // reference: https://stackoverflow.com/questions/105014/does-the-mutable-keyword-have-any-purpose-other-than-allowing-the-variable-to
  188. int test_mutable_4()
  189. {
  190. int x = 0;
  191. auto f1 = [=]() mutable { x = 42; }; // OK
  192. //auto f2 = [=]() { x = 42; }; // Error: a by-value capture cannot be modified in a non-mutable lambda
  193. fprintf( stdout, "x: %d\n", x); // 0
  194. return 0;
  195. }
  196. } // namespace volatile_mutable_

GitHubhttps://github.com/fengbingchun/Messy_Test


转载:https://blog.csdn.net/fengbingchun/article/details/104109696
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场