本文介绍了复制具有原子成员的类的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有原子成员的类,我想写一个复制构造函数:
I have a class with an atomic member and i want to write a copy constructor:
struct Foo
{
std::atomic<int> mInt;
Foo() {}
Foo(const Foo& pOther)
{
std::atomic_store(mInt, std::atomic_load(pOther.mInt, memory_order_relaxed), memory_order_relaxed);
}
};
但是我不知道我必须使用哪种顺序,因为我不知道在何时何地调用此副本构造函数.
But i don't know which ordering i must use, because i don't know where and when this copy constructor will be called.
我可以对复制构造函数和赋值运算符使用relaxed
排序吗?
Can i use relaxed
ordering for copy constructor and assignment operator?
推荐答案
否,如果您不知道如何使用它,则应该使用memory_order_seq_cst
以确保安全.如果使用memory_order_relaxed
,则可能会遇到指令重新排序的问题.
No, if you don't know how it will be used, you should use memory_order_seq_cst
to be safe. If you use memory_order_relaxed
, you could run into issues with instructions being reordered.
这篇关于复制具有原子成员的类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!