- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已阅读 Concurrency: Atomic and volatile in C++11 memory model和 How 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 ofwait()
/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 fornotify_one()
to, for example, be delayed and unblock a thread that started waiting just after the call tonotify_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 tonotify_one()
was made."?
因为所有线程都同意事情发生的顺序,所以您无法在任何地方观察结果先于原因。
关于c++ - std::notify_one() 中的 "a single total order"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53286110/
我读了一些关于 std::condition_variable 的内容更具体地说,如何使用 std::condition_variable::notify_one 通知等待线程。 我遇到了几个问题,我
我有一个关于notify_one函数的问题。在下面的代码中, #include #include #include #include #include std::condition_vari
我对std::condition_variable的使用有点困惑.我知道我必须创建一个 unique_lock在 mutex调用前 condition_variable.wait() .我找不到的是我
这个问题已经有答案了: std::condition_variable::notify_one() called several times without context switching (1
我有一个异步处理任务的“引擎”,对于一个任务,我想等到该任务被处理完。 boost::condition_variable cvWorkDone; DoSomeWork() { PostAsyn
boost::condition_variable::notify_one() 函数是自动恢复执行它解除阻塞的线程,还是未定义并取决于操作系统如何调度线程? 我不太熟悉线程以及操作系统如何与它们交互,
我可以安全地执行以下代码吗?是否有可能在这里出现死锁或一些意外行为,尤其是当 SIGINT 到达时? #include #include #include std::mutex m; std::
条件变量的典型用法如下所示(参见下面的代码):http://en.cppreference.com/w/cpp/thread/condition_variable . 但是,似乎主线程可能会在工作线程
我有如下代码: #include #include #include std::mutex mtx; std::condition_variable cv; std::future worker
据我了解,当我有一组线程受 std::lock_guard 或 std::unique_lock 保护 std::mutex 并且通过显式解锁或锁超出范围解锁互斥锁,然后通知等待线程。 这个通知是 n
本例中有多少等待线程会被唤醒: 第一个线程: void wakeUp2Threads() { std::unique_lock lock(condvar_mutex); condvar
我有一个用于读取/写入的环形缓冲区。我跟踪环形缓冲区中的条目数,并且不允许覆盖尚未读取的条目。我使用 std::condition_variable wait() 和 notify_one() 来同步
我的线程作为链表运行(有重叠,因此线程),实现与时间线没有什么不同的东西。每个条件变量解锁链表中的下一个线程。 当我只有一个要通知时,我应该使用 notify_all() 还是 notify_one(
出于某种原因,调用 signal.notify_one() 会阻塞当前线程并且不会返回。我从未听说过这种行为,也不知道如何解决。 { std::lock_guard lock(_mutex);
在使用 Boost 线程的单一生产者/单一消费者应用程序中,如果生产者线程在消费者线程调用 cond_var 之前多次调用 cond_var.notify_one() 会发生什么情况。等待(锁定) ?
是否需要同步std::condition_variable/condition_variable_any::notify_one? 据我所知,如果丢失通知是可以接受的 - 可以调用未 protecte
考虑第一个线程函数和全局变量: std::mutex mut; std::condition_variable officer; bool firstPlayerIsReady
前言:我在这里看到过类似的问题,但似乎没有一个能回答我的问题。 是否有可靠的方法来确保消费者线程中的 wait() 方法在生产者线程的第一个 notify_one() 调用之前被调用? 即使在消费者线
我正在尝试运行一个 websocket++ 示例,它包含从 websocket 客户端接收消息并广播到所有连接的客户端,但我在线程同步方面遇到问题。 在代码示例中,方法 process_message
我已阅读 Concurrency: Atomic and volatile in C++11 memory model和 How std::memory_order_seq_cst works ,它没
我是一名优秀的程序员,十分优秀!