🐭volatile 的由来与应用

参照论文「C++ and the Perils of Double-Checked Locking」,时光回到 1970 年代,人们发明了一种「内存/端口」映射机制,可以把对硬件特定端口的操作转换为对特定内存地址的操作。
在嵌入式开发中,这个机制还是很常见的,我们拿数码管显示与传感器数据读取来举例说明:
伪代码 :

  1. // DigitalTube pseudo code
  2. uint8_t *p = DigitalTubeAddress();
  3. // write 3 values to DigitalTube
  4. *p = 1;
  5. *p = 2;
  6. *p = 3;
  7. // Sensor pseudo code
  8. uint8_t *p = SensorAddress();
  9. // read 2 values from Sensor
  10. uint8_t a = *p;
  11. uint8_t b = *p;

在上面的伪代码中:
我们把数码管的输出 (显示) 端口映射为一块内存,操作数码管显示不同的数字,就相当于对这块内存写入不同的值,比如依次显示 1,2,3。
我们把传感器的输入 (采集) 端口映射为一块内存,读取传感器的采集值,就相当于读取这块内存,不停的读取会得到不同的采集值,比如依次读取两个值。
这一切都很完美,但是 1970 年代的 C 语言编译器,就具备能力对上面的代码进行优化,优化之后很明显与实际需求不符,可能的优化策略如下:

  1. 使用缓存
  2. 指令重排
  3. 指令优化

为了阻止 C 语言编译器对这种情形下相关代码的优化,C 语言引进了 volatile 关键字,然后 C++ 继承了这个遗产。现在也可以利用 volatile 关键字的特性来做一些事情,另外,不要混淆了 java 中的 volatile 关键字的含义,java 语言实际上大大的扩展了 volatile 关键字的含义。
代码示例 :

  1. #include <iostream>
  2. // volatile keyword in C/C++
  3. void func10() {
  4. int a = 1;
  5. int b = 2;
  6. printf("%d,%d\n", a, b);
  7. // printf("%d,%d\n", 1, 2); (a, b will optimize by literals)
  8. }
  9. // volatile pervents the optimization
  10. void func11() {
  11. volatile int a = 1;
  12. volatile int b = 2;
  13. printf("%d,%d\n", a, b);
  14. }
  15. void func20(void) {
  16. auto p = std::make_unique<int>(42);
  17. *p = 1;
  18. *p = 2;
  19. *p = 3;
  20. // *p = 3; (three line of code, maybe optimize to only one line)
  21. }
  22. // volatile pervents the optimization
  23. void func21(void) {
  24. auto p = std::make_unique<volatile int>(42);
  25. *p = 1;
  26. *p = 2;
  27. *p = 3;
  28. }
  29. void func30() {
  30. int b, y;
  31. int a, x;
  32. b = 1;
  33. y = 2;
  34. a = b;
  35. x = y; // (the last two line of code, maybe reordered)
  36. }
  37. // volatile pervents the optimization
  38. void func31() {
  39. int b, y;
  40. volatile int a, x;
  41. b = 1;
  42. y = 2;
  43. a = b;
  44. x = y;
  45. }
  46. int main() {
  47. func10();
  48. func11();
  49. func20();
  50. func21();
  51. func30();
  52. func31();
  53. return 0;
  54. }

:::tips volatile 关键字是用来阻止代码优化的,主要是为了区分特殊内存的操作与一般内存操作的差异性 ::: volatile 的意义:

  1. 易变性:

volatile 修饰的变量,其内容是易变的,可能存在多个修改源,每次必须从内存中读取内容,不得使用缓存值

  1. 拒接写操作优化:

对 volatile 修饰的变量的写入操作,必须忠实的执行,不得优化掉任何代码语句

  1. 拒接指令重排:

对 volatile 修饰的变量的读写操作,必须忠实的按照它们在代码书写中的顺序执行,不得重排指令

🐂 std::atomic

从上面 volatile 的历史由来可以看出,volatile 是为了操作特殊内存而引入的,我们可以利用它的特性来做一些事情的,但是与多线程编程没有多大关系。
对于单个数据的多线程同步问题,使用 std::atomic 是最合适的,它比锁要高效,简洁,一般我们说的无锁编程就是使用原子变量结合内存模型来实现的。
代码示例 :

  1. #include <iostream>
  2. int main() {
  3. std::cout << std::boolalpha;
  4. // std::std::atomic_int
  5. std::cout << std::atomic_int().is_lock_free() << '\n'; // true
  6. std::cout << std::atomic_int().is_always_lock_free << '\n'; // true
  7. std::atomic<int> ai{42};
  8. // volatile and atomic can be used together
  9. volatile std::atomic<int> vai{42};
  10. // atomicity pre-increment, equivalent to fetch_add(1)+1
  11. ++ai;
  12. ++vai;
  13. // atomicity post-decrement, equivalent to fetch_sub(1)
  14. ai--;
  15. vai--;
  16. std::cout << ai << ' ' << vai << '\n'; // 42 42
  17. int b = 0;
  18. // before memory barrier (std::atomics using memory_order_seq_cst)
  19. ai = 1024; // std::atomic<int> ai, used as a memory barrier
  20. // after memory barrier
  21. int c = 0;
  22. // memory barrier
  23. // 1. all codes before memory barrier
  24. // can not reordered to after the memory barrier code
  25. // 2. all codes after memory barrier
  26. // can not reordered to before the memory barrier code
  27. // for example:
  28. // "int b = 0;" must execute before "int c = 0;"
  29. return 0;
  30. }

🐯 总结

  1. volatile 是为了操作特殊内存而生,是为了阻止编译器代码优化的
  2. std::atomic 与内存模型一起是为了多线程而生,在多线程中可以无锁的安全的来操作原子变量,注意原子变量内存模型参数的选取