代码之家  ›  专栏  ›  技术社区  ›  Wonter

原子总是锁着的吗?

  •  4
  • Wonter  · 技术社区  · 6 年前

    atomic<T*> 没有锁。

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

    我想知道 原子的;

    有人介绍吗?

    2 回复  |  直到 6 年前
        1
  •  9
  •   VLL Mohamed El-Nakeep    6 年前

    The standard allows implementing any atomic type (with exception of std::atomic_flag) to be implemented with locks. 即使平台允许某些类型的无锁原子,标准库开发人员也可能没有实现。

    如果在使用锁时需要实现一些不同的东西,可以在编译时使用 ATOMIC_POINTER_LOCK_FREE

        2
  •  2
  •   Caleth    6 年前

    不,假设任何特定平台的 std::atomic 总是没有锁。

    ATOMIC_POINTER_LOCK_FREE ,这表示指针对于所讨论的平台永远、有时或始终是不可用的。

    std::atomic<T *>::is_always_lock_free ,特别是 T . 1个

    must be consistent ,所以instance方法 std::atomic<T *>::is_lock_free()