- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有如下代码:
#include <mutex>
#include <condition_variable>
#include <future>
std::mutex mtx;
std::condition_variable cv;
std::future<void> worker;
void worker_thread() {
{
std::lock_guard<std::mutex> lg{ mtx };
// do something 1
}
cv.notify_one();
// do something 2
}
int main() {
{
std::unique_lock<std::mutex> lg{ mtx };
worker = std::async(std::launch::async, worker_thread);
cv.wait(lg);
}
// do something 3
}
主线程没有继续执行 //do something 3
我不明白为什么。我认为 cv.notify_one()
行应该在 cv.wait(lg)
被主线程传递后从工作线程到达,所以没有理由挂起来。
工作线程负责一些流式数据处理,而主线程主要负责GUI事件处理。
//do something 1
是关于一些应该在工作线程内完成的初始化。主线程应该等待工作线程完成它。
//do something 2
是工作线程的主要工作。
//do something 3
是主线程的主要工作。
将 cv.notify_one()
更改为 cv.notify_all()
没有帮助。
条件变量的这种用法是否正确?
最佳答案
我不得不回到原来的答案,为此我向 Junekey 道歉。我误读了代码,并断定存在竞争条件。我无法重现该问题。我们需要一个实际上永远阻塞在 cv.wait 上的示例,以便弄清楚它为什么这样做。然而,如果没有其他原因,代码是不正确的,它可能会收到虚假通知并在 worker_thread 调用 cv.notify 之前通过 cv.wait。这种情况很少发生,但确实会发生。
此代码或多或少是规范的:
#include <mutex>
#include <condition_variable>
#include <thread>
std::mutex mtx;
std::condition_variable cv;
bool worker_done = false; // <<< puts the "condition" in condition_variable
void worker_thread() {
// do something 1
{
std::lock_guard<std::mutex> lg{ mtx };
worker_done = true; // ... and whatever
}
cv.notify_one();
// do something 2
}
int main() {
std::thread workman(worker_thread);
{
std::unique_lock<std::mutex> lg{ mtx };
while (!worker_done) {
cv.wait(lg);
}
}
// do something 3
workman.join();
}
关于c++ - std::condition_variable::notify_one() 不唤醒等待线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48227174/
我读了一些关于 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 ,它没
我是一名优秀的程序员,十分优秀!