C++0x 原子模板实现

标签 c++ synchronization c++11

我知道在 Intel 的 TBB 中存在类似的模板,除此之外我在 google 或 Boost 库中找不到任何实现。

最佳答案

您可以在 boost 中找到有关此功能实现的讨论:http://lists.boost.org/Archives/boost/2008/11/144803.php

> Can the N2427 - C++ Atomic Types and Operations be implemented

> without the help of the compiler?

No.

They don't need to be intrinsics if you can write inline assembler (or separately-compiled assembler for that matter) then you can write the operations themselves directly. You might even be able to use simple C++ (e.g. just plain assignment for load or store). The reason you need compiler support is preventing inappropriate optimizations: atomic operations can't be optimized out, and generally must not be reordered before or after any other operations. This means that even non-atomic stores performed before an atomic store have to be complete, and can't be cached in a register (for example). Also, loads that occur after an atomic operation cannot be hoisted before the atomic op. On some compilers, just using inline assembler is enough. On others, calling an external function is enough. MSVC provides _ReadWriteBarrier() to provide the compiler ordering. Other compilers need other flags.

关于C++0x 原子模板实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/523827/

相关文章:

c++ - 函数调用中char[]和char*的区别

ios - 使用 AVCaptureSession 和 AVAssetWriter 在翻转相机的同时进行无缝录音

c++ - 使用模板在编译时填充运行时数据

c++ - std::generate_n 无法生成我的对象

c++ - 如何在循环外重新输入变量

java - 从 JVM 清除所有加载的类

c++ - 无法在 OSX Yosemite (Macbook 8,1) 中运行 OpenCV 示例

java - 在可重入锁中等待条件

synchronization - Vulkan 中统一缓冲区的最佳实践

c++ - 具有右值删除器的 unique_ptr 构造函数返回 null?