gpt4 book ai didi

c++ - 请解释在c++线程中条件变量的使用,以及为什么我们需要同时使用 `unique_lock`和 `mutex`

转载 作者:太空狗 更新时间:2023-10-29 19:56:22 47 4
gpt4 key购买 nike

我指的是这段特定的代码:

这段代码基本上是三个线程1. 与服务器进行一些握手2. 从 XML 文件加载数据。3. 对从 XML 加载的数据进行处理。正如我们所看到的,任务 1 不依赖于任何其他任务,但任务 3 依赖于任务 2。因此,这意味着任务 1 和任务 2 可以由不同的线程并行运行以提高应用程序的性能。因此,应用程序被构建为多线程。

#include <iostream>
#include <thread>
#include <functional>
#include <mutex>
#include <condition_variable>
using namespace std::placeholders;

class Application
{
std::mutex m_mutex;
std::condition_variable m_condVar;
bool m_bDataLoaded;
public:
Application()
{
m_bDataLoaded = false;
}
void loadData()
{
// Make This Thread sleep for 1 Second
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::cout<<"Loading Data from XML"<<std::endl;
// Lock The Data structure
std::lock_guard<std::mutex> guard(m_mutex);
// Set the flag to true, means data is loaded
m_bDataLoaded = true;
// Notify the condition variable
m_condVar.notify_one();
}
bool isDataLoaded()
{
return m_bDataLoaded;
}
void mainTask()
{
std::cout<<"Do Some Handshaking"<<std::endl;
// Acquire the lock
std::unique_lock<std::mutex> mlock(m_mutex);
// Start waiting for the Condition Variable to get signaled
// Wait() will internally release the lock and make the thread to block
// As soon as condition variable get signaled, resume the thread and
// again acquire the lock. Then check if condition is met or not
// If condition is met then continue else again go in wait.
m_condVar.wait(mlock, std::bind(&Application::isDataLoaded, this));
std::cout<<"Do Processing On loaded Data"<<std::endl;
}
};
int main()
{
Application app;
std::thread thread_1(&Application::mainTask, &app);
std::thread thread_2(&Application::loadData, &app);
thread_2.join();
thread_1.join();
return 0;
}

这段代码来自 http://thispointer.com/c11-multithreading-part-7-condition-variables-explained/

谢谢

最佳答案

条件变量允许一个人以原子方式释放持有的互斥量并使线程进入休眠状态。然后,在收到信号后,以原子方式重新获取互斥量并唤醒。例如,您会在生产者/消费者问题中遇到这种情况。如果您在持有互斥量的同时进入休眠状态,您死锁,但如果您在休眠前释放它(通过错过唤醒信号),您也可能会死锁。

这不是没有例子就可以用几段话来解释的事情,使用条件变量有几个众所周知的陷阱和注意事项。查看 Andrew D. Birrell 的“线程编程简介”。

无论使用何种语言,条件变量始终采用互斥体。调用 wait 时必须保持互斥量。从等待返回后,您应该始终验证所需条件是否仍然为真。这就是为什么您总是看到有条件的等待包含在 while 循环中。 C++11 还为您提供谓词重载,这是 while 循环的语法糖。

互斥体保护共享状态。该条件允许您阻塞直到收到信号。

unique_lock 是用于锁定和解锁给定互斥体的 RAII(资源获取即初始化)包装器。它在概念上与 C# 中的 lock 语句相同。它通过将互斥量获取和释放与 unique_lock 实例的生命周期联系起来,简化了异常处理。我不知道 condition_variable 是否有理由强制您使用它,而不是因为它是一种很好的做法。 unique_locklock_guard 之间的唯一区别是 unique_lock 可以解锁......这就是为什么你必须使用它而不是 lock_guardcondition_variable

关于c++ - 请解释在c++线程中条件变量的使用,以及为什么我们需要同时使用 `unique_lock`和 `mutex`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50331130/

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