标准::原子<uint_least8_t>行为
std::atomic<uint_least8_t> behavior
在具有以下功能的系统上:
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
据我了解,类型
std::atomic<uint_least8_t>
将是 8 位并且不解锁。
如果是这样,如果我想要一个至少 8 位并且始终无锁的原子类型,我应该写什么?(假设存在此类类型(有没有比以下更好的选择:
std::atomic<
typename std::conditional<
std::atomic<uint8_t>::is_always_lock_free,
uint8_t,
uint16_t
>::type
>
(为简单起见,我没有包含未解锁if std::atomic<uint16_t>
代码(
好吧,模板的东西有点混乱,但它的用法非常直观:
#include <atomic>
#include <cstdint>
#include <iostream>
template <typename T, typename... others>
class first_lockfree
{
// forward declare internal template
template <bool, typename...> struct cond;
// use this if is_always_lock_free == true
template <typename condT, typename... condTs>
struct cond<true, condT, condTs...>
{
// use first template argument as it is_always_lock_free
using type = condT;
};
// use this if is_always_lock_free == false
template <typename condT, typename... condTs>
struct cond<false, condT, condTs...>
{
// use main template with first template parameter removed
using type = typename first_lockfree<condTs...>::type;
};
public:
using type =typename cond<std::atomic<T>::is_always_lock_free, T, others...>::type;
};
int main(int, char**)
{
using uint8_lockfree_t = first_lockfree<std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t>::type;
std::cout << sizeof(uint8_lockfree_t) << std::endl;
std::cout << std::atomic<std::uint8_t>::is_always_lock_free << std::endl;
std::cout << std::atomic<std::uint16_t>::is_always_lock_free << std::endl;
std::cout << std::atomic<std::uint32_t>::is_always_lock_free << std::endl;
std::cout << std::atomic<std::uint64_t>::is_always_lock_free << std::endl;
return 0;
}
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- arr[-1]在c++中的奇怪行为
- 继承期间显示未知行为的子类
- 如何在c++中使用引用实现类似python的行为
- G锁定铸造到基础上会释放模拟行为
- 在C++中对T*类型执行std::move的意外行为
- std::当在256字节边界上写入整数时,流的奇怪行为
- 不知道某个东西是否被忽略会引入未定义的行为吗
- 奇怪的构造函数行为
- 重载运算符new[]的行为取决于析构函数
- 不同语言中相同代码的不同行为
- 处理除以零会导致<csignal>意外行为
- 试图理解类对象的行为
- EASTL矢量<向量<int>>连续的
- c++11评估顺序(未定义的行为)
- 从结构寻址时,MMAP变量的行为很奇怪
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 读取文件时运行时的未知行为
- strncpy之后的char数组的错误行为