std::atomic变量应该使用"normal"语法还是"load"和"store"?
Should std::atomic variables use "normal" syntax or "load" and "store"?
如果我有一个原子变量,例如
std::atomic<int> x;
并且我想对其进行读写操作,我可以使用"正常"语法,例如
std::cout << x; // read from x
x = 5; // write to x
我也可以使用显式的load
和store
成员函数:
std::cout << x.load(); // read from x
x.store(5); // write to x
我看到像Andrei Alexandrescu和Anthony Williams这样的人建议只使用显式的load
和store
形式,大概是因为"正常"形式不强调变量是原子的。这看起来像是匈牙利符号的一种形式。读写原子时使用的语法是否有新的约定?
有几个操作被重载为"做你想做的事",并且使用顺序一致的内存排序来做到这一点。所以:
-
int n = x;
与int n = x.load(std::memory_order_seq_cst)
相同, -
x = 1
与x.store(1, std::memory_order_seq_cst)
相同。
然而,如果你想要任何一种宽松的内存排序,你需要使用显式成员函数,例如int n = x.load(std::memory_order_acquire)
。
这个想法是,看起来"自然"的代码通常是正确的(回想一下"无数据竞争的程序的顺序一致"执行模型),但风险更大,更激进的操作是可用的& & # ash;
相关文章:
- 调用原子的 store() 时可以调用基类型类的函数吗?C++
- 使用 Outlook 2016 Microsoft Store 连接到 Outlook COM
- 为什么 Glib::VariantBase::store 方法破坏了给定缓冲区的开始
- DRD 报告"conflicting load" std::mutex::lock 上的错误
- numpy.load 给出 ValueError: descr 不是有效的 dtype 描述符:
- Linux non-persistent backing store for mmap()
- Qt3D:"Mesh is empty, nothing to load"消息
- Java 1.8 本机 System.load 通过 JNI 调用C++产生 TLSv1.如何获取 TLSv1.2?
- 无法将 IOKit 驱动程序与 IOHIDFamily 链接:"dependency load failed"
- 使用RCPP包创建R包时出错:macOS中的dyn.load中出错
- 当使用来自 std::atomic 的方法时隐式调用 load() 吗?
- 无法在模板中"store member function"
- 使用 Python "cPickle.load"加载C++编写的二进制文件时的 EOFError
- atomic.store 和atomic_thread_fence有什么区别?
- “boost::p rogram_options”不能在“variables_map”上使用两次“store”
- Visual C++ Load rtf 文档在丰富编辑框中包含图像和文本
- boost::p rogram_option::store 在选项字符串包含混合语言字符时引发异常
- R dyn.load "Symbol not found"错误,即使C++代码构建良好
- 无法使用 dyn.load windows 7 64 位在 R 中加载 dll 文件
- std::atomic变量应该使用"normal"语法还是"load"和"store"?