在一个系统上:
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
std::atomic<uint8_t>::is_always_lock_free // => false
std::atomic<uint16_t>::is_always_lock_free // => true
Run Code Online (Sandbox Code Playgroud)
从我的理解,类型
std::atomic<uint_least8_t>
Run Code Online (Sandbox Code Playgroud)
将是8位而不是无锁.
如果是这样,如果我想要一个至少8位的原子类型并且总是锁定自由,我该怎么写?(假设存在这种类型)是否有比以下更好的选择:
std::atomic<
typename std::conditional<
std::atomic<uint8_t>::is_always_lock_free,
uint8_t,
uint16_t
>::type
>
Run Code Online (Sandbox Code Playgroud)
(为简单起见,我没有包含代码if std::atomic<uint16_t>不锁定)