gpt4 book ai didi

c++ - std::notify_one() 中的 "a single total order"是什么意思?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:49:57 26 4
gpt4 key购买 nike

我已阅读 Concurrency: Atomic and volatile in C++11 memory modelHow std::memory_order_seq_cst works ,它没有多大帮助,直接回答我的问题。


来自 https://en.cppreference.com/w/cpp/thread/condition_variable/notify_one :

The effects of notify_one()/notify_all() and each of the three atomic parts of wait()/wait_for()/wait_until() (unlock+wait, wakeup, and lock) take place in a single total order that can be viewed as modification order of an atomic variable: the order is specific to this individual condition_variable. This makes it impossible for notify_one() to, for example, be delayed and unblock a thread that started waiting just after the call to notify_one() was made.

“以单一总顺序发生”是什么意思?这与下一句“This makes it impossible ..... was made.”有什么关系? (好像是在说因果)。

我一个字一个字地读了 10 多遍,不明白它在说什么。维基百科上的“总顺序”定义帮不上什么忙。

最佳答案

What does it mean by saying "take place in a single total order"?

这意味着每个线程看到相同的操作序列。例如,使用多个非原子变量,线程 C 可以先看到线程 A 对 int a 的更改,然后再看到线程 B 对 int b 的更改,而线程 D 在 A 之前看到 B 的那些。有多个不兼容的时间线,其中事件发生在其他事件之前,可能每个线程都不同意另一个。如果没有同步机制(如 std::condition_variable),就不可能防止不需要的系统行为。

全序 意味着每个元素都可以与其他所有元素进行比较(对比偏序,其中某些元素对是无法比较的)。在这种情况下,存在事件的时间线。它是单一的,因为所有线程都同意它。

How is this related to the next sentence "This makes it impossible for notify_one() to, for example, be delayed and unblock a thread that started waiting just after the call to notify_one() was made."?

因为所有线程都同意事情发生的顺序,所以您无法在任何地方观察结果先于原因。

关于c++ - std::notify_one() 中的 "a single total order"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53286110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com