假设对齐指针加载和存储在目标平台上自然是原子的,这有什么区别:
// Case 1: Dumb pointer, manual fence
int* ptr;
// ...
std::atomic_thread_fence(std::memory_order_release);
ptr = new int(-4);
这个:
// Case 2: atomic var, automatic fence
std::atomic<int*> ptr;
// ...
ptr.store(new int(-4), std::memory_order_release);
还有这个:
// Case 3: atomic var, manual fence
std::atomic<int*> ptr;
// ...
std::atomic_thread_fence(std::memory_order_release);
ptr.store(new int(-4), std::memory_order_relaxed);
我的印象是它们都是等价的,但是 Relacy检测到数据竞争 第一种情况(仅):
struct test_relacy_behaviour : public rl::test_suite<test_relacy_behaviour, 2>
{
rl::var<std::string*> ptr;
rl::var<int> data;
void before()
{
ptr($) = nullptr;
rl::atomic_thread_fence(rl::memory_order_seq_cst);
}
void thread(unsigned int id)
{
if (id == 0) {
std::string* p = new std::string("Hello");
data($) = 42;
rl::atomic_thread_fence(rl::memory_order_release);
ptr($) = p;
}
else {
std::string* p2 = ptr($); // <-- Test fails here after the first thread completely finishes executing (no contention)
rl::atomic_thread_fence(rl::memory_order_acquire);
RL_ASSERT(!p2 || *p2 == "Hello" && data($) == 42);
}
}
void after()
{
delete ptr($);
}
};
我联系了 Relacy 的作者,以了解这是否是预期的行为;他说我的测试用例中确实存在数据竞争。 但是,我很难发现它;有人可以向我指出比赛是什么吗? 最重要的是,这三种情况有什么区别?
更新:我突然想到,Relacy 可能只是在提示跨线程访问的变量的原子性(或者说缺乏原子性)......之后总而言之,它不知道我打算只在对齐整数/指针访问自然是原子的平台上使用此代码。
另一个更新:Jeff Preshing 写了一篇出色的博文 explaining the difference between explicit fences and the built-in ones (“围栏”与“操作”)。案例 2 和 3 显然不等价! (无论如何,在某些微妙的情况下。)
最佳答案
尽管各种答案都涵盖了潜在问题的部分内容和/或提供了有用的信息,但没有一个答案能正确描述所有三种情况的潜在问题。
为了在线程之间同步内存操作,使用release和acquire屏障来指定排序。
在图中,线程 1 中的内存操作 A 不能向下移动穿过(单向)释放屏障(无论这是否是对原子存储的释放操作,
或一个独立的发布围栏,然后是一个轻松的原子存储)。因此,内存操作 A 保证在原子存储之前发生。
线程 2 中的内存操作 B 也是如此,它不能向上移动越过获取屏障;因此原子负载发生在内存操作 B 之前。
原子ptr
本身提供线程间排序,基于它具有单一修改顺序的保证。一旦线程 2 看到 ptr
的值,
保证存储(以及内存操作 A)发生在加载之前。因为负载保证发生在内存操作 B 之前,
传递性规则说内存操作 A 在 B 之前发生并且同步完成。
有了这个,让我们看看你的 3 个案例。
案例 1 被破坏,因为 ptr
是一种非原子类型,在不同的线程中被修改。这是数据竞争的经典示例,它会导致未定义的行为。
情况 2 是正确的。 作为参数,使用 new
的整数分配在释放操作之前排序。这相当于:
// Case 2: atomic var, automatic fence
std::atomic<int*> ptr;
// ...
int *tmp = new int(-4);
ptr.store(tmp, std::memory_order_release);
案例 3 被破坏了,尽管是以一种微妙的方式。问题是即使 ptr
赋值在独立栅栏之后正确排序,
整数分配(new
)也在栅栏之后排序,导致整数内存位置上的数据竞争。
代码相当于:
// Case 3: atomic var, manual fence
std::atomic<int*> ptr;
// ...
std::atomic_thread_fence(std::memory_order_release);
int *tmp = new int(-4);
ptr.store(tmp, std::memory_order_relaxed);
如果您将其映射到上图,则 new
运算符应该是内存操作 A 的一部分。在释放栅栏下方进行排序,
排序保证不再成立,整数分配实际上可以使用线程 2 中的内存操作 B 重新排序。
因此,线程 2 中的 load()
可能会返回垃圾或导致其他未定义的行为。
关于c++ - 使用显式栅栏和 std::atomic 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14167767/