简体   繁体   English

为什么不能 std::atomic<t> 被交换?</t>

[英]Why can't std::atomic<T> be swapped?

#include <atomic>

int main()
{
    auto a = std::atomic_int(1);
    auto b = std::atomic_int(2);

    std::swap(a, b); // error
}

error message:错误信息:

error: no matching function for call to 'swap(std::atomic&, std::atomic&)'错误:没有匹配的 function 调用 'swap(std::atomic&, std::atomic&)'

Why can't std::atomic<T> be swapped?为什么不能交换std::atomic<T>

std::atomic has a deleted copy constructor, and doesn't have a move construtor. std::atomic有一个已删除的复制构造函数,并且没有移动构造函数。

Therefore it is neither move assignable nor move constructible .因此它既不是move assignable也不是move constructible

Therefore std::swap cannot be called on any std::atomic type.因此std::swap不能在任何std::atomic类型上调用。

Reference:参考:

https://en.cppreference.com/w/cpp/algorithm/swap https://en.cppreference.com/w/cpp/algorithm/swap

There are two levels to that issue.这个问题有两个层面。

First is plain simple and technical - std::atomic is not move constructible or move assignable as mentioned in other answer.首先是简单且技术性的 - std::atomic不可移动构造或移动可分配,如其他答案中所述。

Second is the rationale behind this - swapping std::atomic s would not be atomic in itself.其次是这背后的基本原理——交换std::atomic本身并不是原子的。 And since std::atomic s are used in multithreaded environments adding swap would have lead to wide range of bugs due to possible misunderstandings (that since there is swap for std::atomic then it is atomic in itself).并且由于std::atomic在多线程环境中使用,添加swap会由于可能的误解而导致广泛的错误(因为std::atomicswap ,所以它本身就是原子的)。

All in all - if you don't need atomic swap this can be pretty easily done using mentioned exchange s.总而言之——如果你不需要原子swap ,这可以很容易地使用提到的exchange来完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么TBB无法将`int`转换为`const tbb :: atomic <unsigned int> &`,但std :: atomic可以吗? - Why can't TBB cast `int` to `const tbb::atomic<unsigned int>&`, but std::atomic can? 为什么std :: atomic initialisation没有原子释放,所以其他线程可以看到初始值? - Why doesn't std::atomic initialisation do atomic release so other threads can see the initialised value? 为什么不能使用 std::atomic_bool 隐式转换为 std::variant - why can't implicitly convert to a std::variant with std::atomic_bool 为什么不能默认构造 std::chrono time_point 成员变量的 std::atomic ? - Why can't a std::atomic of a std::chrono time_point member variable be default constructed? 为什么std :: atomic_ {char,schar等} typedef允许是std :: atomic <T>的基类的typedef,而不是仅仅是atomic <T>? - Why are the std::atomic_{char,schar,etc.} typedefs allowed to be typedefs to a base class of std::atomic<T>, and not to atomic<T> only? 如何从 std::atomic 中提取指针 T<t> ?</t> - How can I extract the pointer T from std::atomic<T>? sig_atomic_t和std :: atomic &lt;&gt;是否可以互换? - Are sig_atomic_t and std::atomic<> interchangable? 为什么编译器没有合并多余的std :: atomic写入? - Why don't compilers merge redundant std::atomic writes? 几个std :: atomic <T>上的条件 - condition on several std::atomic<T> 为什么std :: atomic_is_lock_free不是静态constexpr? - Why isn't std::atomic_is_lock_free a static constexpr?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM