c++ - 为什么 is_lock_free 是成员函数?

标签 c++ c++11 std atomic

is_lock_free 需要实例(它是成员函数)的原因是什么?为什么不是该类型的元函数,或者静态 constexpr 成员函数?

我正在寻找一个实际的例子来说明为什么它是必要的。

最佳答案

标准允许类型有时无锁。

section 29.4 Lock-free property

The ATOMIC_..._LOCK_FREE macros indicate the lock-free property of the corresponding atomic types, with the signed and unsigned variants grouped together. The properties also apply to the corresponding (partial) specializations of the atomic template. A value of 0 indicates that the types are never lock-free. A value of 1 indicates that the types are sometimes lock-free. A value of 2 indicates that the types are always lock-free.

C++ atomic paper n2427说明背后的原因:

... The proposal provides run-time lock-free query functions rather than compile-time constants because subsequent implementations of a platform may upgrade locking operations with lock-free operations, so it is common for systems to abstract such facilities behind dynamic libraries, and we wish to leave that possiblility open. Furthermore, we recommend that implementations without hardware atomic support use that technique. ...

还有(正如 Jesse Good 所指出的):

The proposal provides lock-free query functions on individual objects rather than whole types to permit unavoidably misaligned atomic variables without penalizing the performance of aligned atomic variables

关于c++ - 为什么 is_lock_free 是成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10407514/

相关文章:

c++ - 是否有 std::sum 更新的实现

c++ stof()函数在多次工作后出错

c++ - 检查字符串字符和长度

c++ - 大小如何工作?我怎么写自己的?

c++ - 为枚举类重载强制转换运算符

c++ - 模板化类型转换运算符和偏特化

c++ - 在没有 clang 警告的情况下在 C++11 中非平凡地初始化模板类的静态成员

c++ - 访问控制对已删除的构造函数是否重要?

c++ - 安卓工作室 : inconsistent automatic including of C++ stdlib headers

c++ - 在尚未从模板中声明的命名空间中使用函数