用于原子读/写值的静态断言

发布于 2024-10-30 03:26:19 字数 171 浏览 1 评论 0原文

有没有办法检查值的读/写(加载/存储)是否是原子的?我有专门版本的并发容器,只能使用这些值,我想添加一个静态断言以防止意外误用。

对于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-残月青衣踏尘吟 2024-11-06 03:26:19

C++0x 草案在中有一个包含宏的部分。 header,这表明没有简单且可移植的方法来检查它。

29.4 无锁属性 [atomics.lockfree]

#define ATOMIC_CHAR_LOCK_FREE implementation-defined 
#define ATOMIC_CHAR16_T_LOCK_FREE implementation-defined 
#define ATOMIC_CHAR32_T_LOCK_FREE implementation-defined 
#define ATOMIC_WCHAR_T_LOCK_FREE implementation-defined 
#define ATOMIC_SHORT_LOCK_FREE implementation-defined 
#define ATOMIC_INT_LOCK_FREE implementation-defined 
#define ATOMIC_LONG_LOCK_FREE implementation-defined 
#define ATOMIC_LLONG_LOCK_FREE implementation-defined

宏指示 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.

29.4 Lock-free property [atomics.lockfree]

#define ATOMIC_CHAR_LOCK_FREE implementation-defined 
#define ATOMIC_CHAR16_T_LOCK_FREE implementation-defined 
#define ATOMIC_CHAR32_T_LOCK_FREE implementation-defined 
#define ATOMIC_WCHAR_T_LOCK_FREE implementation-defined 
#define ATOMIC_SHORT_LOCK_FREE implementation-defined 
#define ATOMIC_INT_LOCK_FREE implementation-defined 
#define ATOMIC_LONG_LOCK_FREE implementation-defined 
#define ATOMIC_LLONG_LOCK_FREE implementation-defined

The macros indicates the types where std::atomic<type> can be implemented without a lock, which means that they are atomic in themselves.

治碍 2024-11-06 03:26:19

您拥有的唯一方法是转储每个函数生成的程序集,并参考处理器供应商的说明以获取指令原子性保证。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文