Reputation: 343
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?
Upvotes: 7
Views: 1931
Reputation: 63152
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.
Upvotes: 2
Reputation: 10185
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.
Upvotes: 10