简体   繁体   中英

Is atomic<T*> always lock free?

On my MAC OS, atomic<T*> is lock free.

#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?

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. 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.

No, it is not safe to assume that any particular platform's implementation of std::atomic is always lock free.

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.

You can also get an answer from std::atomic<T *>::is_always_lock_free , for your particular T . 1

Note 1: A given pointer type must be consistent , so the instance method std::atomic<T *>::is_lock_free() is redundant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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