用于原子读/写值的静态断言
有没有办法检查值的读/写(加载/存储)是否是原子的?我有专门版本的并发容器,只能使用这些值,我想添加一个静态断言以防止意外误用。
对于 x86_64 上的所有基本类型都是如此,但对于所有平台或所有长数据类型可能并非如此。此外,小型结构和联合也可能被分配原子操作(因为它们只是被编译为使用相同大小的基本复制操作)。
Is there a way to check if the reading/writing (load/store) of a value is atomic? I have specialized versions of concurrent containers than can only work with such values and I would like to add a static assert to prevent accidental misuse.
For all the fundamental types on x86_64 this is true, but it may not be true for all platforms, or all long data types. Also, it is possible that small structures and unions will also be assigned with an atomic operation (since they'd just be compiled to use the same-size fundamental copy operation).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C++0x 草案在中有一个包含宏的部分。 header,这表明没有简单且可移植的方法来检查它。
宏指示 std::atomic的类型。可以在没有锁的情况下实现,这意味着它们本身是原子的。
The C++0x draft has a section with macros in the <atomic> header, which indicates that there is no easy and portable way to check this otherwise.
The macros indicates the types where std::atomic<type> can be implemented without a lock, which means that they are atomic in themselves.
您拥有的唯一方法是转储每个函数生成的程序集,并参考处理器供应商的说明以获取指令原子性保证。
The only method you have is to dump the generated assembly of each function and refer to the processor vendor's notes for the instructions atomicity guarantees.