简体   繁体   English

多个读取器可以通过获取/释放顺序与相同的写入器同步吗?

[英]Can multiple readers synchronize with the same writers with acquire/release ordering?

After reading "Concurrency in action" I could not find an answer to one question - are there guarantees in the standard for reading side effects when we have one store(release) and many loads(acquire) on one atomic variable?在阅读了“Concurrency in action”之后,我找不到一个问题的答案——当我们在一个原子变量上有一个存储(发布)和许多加载(获取)时,标准中是否有读取副作用的保证? Suppose we have:假设我们有:

int i{};
atomic<bool> b{};

void writer(){
 i=42;
 b.store(true,memory_order_release);
}

void reader(){
 while(!b.load(memory_order_acquire))
    this_thread::yield();
 assert(i==42);
}
//---------------------
thread t1{writer},t2{reader},t3{reader};

If we had only one reader, all ok, but can we have failed assertion in t2 or t3 thread?如果我们只有一个读者,一切都好,但是我们会在 t2 或 t3 线程中断言失败吗?

This is a text-book example of a happens-before relationship between the store to i in writer and the load from i in both reader threads.这是一个教科书示例,说明了在writer store 到i与在两个reader线程中从i加载之间的关系。

That multiple readers are involved does not matter.涉及多个读者并不重要。 The store to b synchronizes with all readers that observe the updated value (which will eventually happen thanks to the loop).b的存储与观察更新值的所有读取器同步(由于循环,这最终会发生)。

I think the quote you're looking for is:我认为您正在寻找的报价是:

An atomic operation A that performs a release operation on an atomic object M synchronizes with an atomic operation B that performs an acquire operation on M and takes its value from any side effect in the release sequence headed by A.对原子对象 M 执行释放操作的原子操作 A 与对 M 执行获取操作的原子操作 B 同步,并从以 A 为首的释放序列中的任何副作用中获取其值。

It does not say that this is limited to a single load operation它并不是说这仅限于单个加载操作

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM