简体   繁体   English

是原子的 <T*> 永远锁定免费?

[英]Is atomic<T*> always lock free?

On my MAC OS, atomic<T*> is lock free. 在我的MAC OS上, atomic<T*>是无锁的。

#include <iostream>
#include <atomic>

int main() {
    std::cout << std::atomic<void*>().is_lock_free() << std::endl;
    return 0;
}

output: 1

I want to know if atomic<T*> is always lock free? 我想知道atomic<T*>是否总是无锁?

Is there a reference to introduce it? 有参考介绍吗?

The standard allows implementing any atomic type (with exception of std::atomic_flag) to be implemented with locks. 该标准允许使用锁实现任何原子类型(std :: atomic_flag除外)。 Even if the platform would allow lock-free atomics for some type, the standard library developers might not have implemented that. 即使平台允许某些类型的无锁原子,标准库开发人员也可能没有实现。

If you need to implement something differently when locks are used, this can be checked at compile time using ATOMIC_POINTER_LOCK_FREE macro. 如果在使用锁时需要以不同的方式实现某些内容,可以在编译时使用ATOMIC_POINTER_LOCK_FREE宏来检查。

No, it is not safe to assume that any particular platform's implementation of std::atomic is always lock free. 不,假设任何特定平台的std::atomic实现总是无锁是不安全的。

The standard specifies some marker macros, including ATOMIC_POINTER_LOCK_FREE , which indicates either pointers are never, sometimes or always lock free, for the platform in question. 该标准指定了一些标记宏,包括ATOMIC_POINTER_LOCK_FREE ,它指示对于所讨论的平台,指针永远不会,有时或总是无锁。

You can also get an answer from std::atomic<T *>::is_always_lock_free , for your particular T . 您还可以从std::atomic<T *>::is_always_lock_free获取特定T的答案。 1 1

Note 1: A given pointer type must be consistent , so the instance method std::atomic<T *>::is_lock_free() is redundant. 注1:给定的指针类型必须一致 ,因此实例方法std::atomic<T *>::is_lock_free()是多余的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM