我有一个带有原子成员的类,我想编写一个复制构造函数:
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);
}
};但是我不知道我必须使用哪种排序,因为我不知道何时何地会调用这个复制构造函数。
我可以对复制构造函数和赋值操作符使用relaxed排序吗?
发布于 2015-10-04 21:16:59
如果您的复制操作应该与其他线程上的其他操作同步,则只需要比memory_order_relaxed更强的内存排序。
然而,这种情况几乎从未发生过,因为线程安全的复制构造函数几乎总是需要一些外部同步或额外的互斥对象。
https://stackoverflow.com/questions/19961043
复制相似问题