C++ 条件变量的用途
不带std::条件_变量的应用程序:C++ 条件变量的用途,c++,condition-variable,stdthread,C++,Condition Variable,Stdthread,不带std::条件_变量的应用程序: #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <queue> #include <chrono> std::mutex mutex; std::queue<int> queue; int counter; void loadData
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <chrono>
std::mutex mutex;
std::queue<int> queue;
int counter;
void loadData()
{
while(true)
{
std::unique_lock<std::mutex> lock(mutex);
queue.push(++counter);
lock.unlock();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void writeData()
{
while(true)
{
std::lock_guard<std::mutex> lock(mutex);
while(queue.size() > 0)
{
std::cout << queue.front() << std::endl;
queue.pop();
}
}
}
int main()
{
std::thread thread1(loadData);
std::thread thread2(writeData);
thread1.join();
thread2.join();
return 0;
}
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <chrono>
std::mutex mutex;
std::queue<int> queue;
std::condition_variable condition_variable;
int counter;
void loadData()
{
while(true)
{
std::unique_lock<std::mutex> lock(mutex);
queue.push(++counter);
lock.unlock();
condition_variable.notify_one();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void writeData()
{
while(true)
{
std::unique_lock<std::mutex> lock(mutex);
condition_variable.wait(lock, [](){return !queue.empty();});
std::cout << queue.front() << std::endl;
queue.pop();
}
}
int main()
{
std::thread thread1(loadData);
std::thread thread2(writeData);
thread1.join();
thread2.join();
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
std::互斥互斥;
std::队列;
整数计数器;
void loadData()
{
while(true)
{
std::唯一锁(互斥锁);
queue.push(++计数器);
lock.unlock();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
无效写入数据()
{
while(true)
{
std::锁和保护锁(互斥锁);
while(queue.size()>0)
{
std::cout第二个代码是安全的,因为对wait(lock,pred)
的调用相当于(直接来自标准):
调用等待(锁定)
释放(解锁)锁定
,并在收到通知时重新获取(锁定)
在您的情况下,这相当于:
auto pred = [](){return !queue.empty();};
std::unique_lock<std::mutex> lock(mutex); // acquire
while (!pred) { // Ok, we are locked
condition_variable.wait(lock); // release
// if you get here, the lock as been re-acquired
}
autopred=[](){return!queue.empty();};
std::unique_lock lock(互斥);//获取
当(!pred){//好的,我们被锁定了
条件变量。等待(锁定);//释放
//如果你到了这里,锁就被重新获得了
}
因此,对pred
的所有调用都是使用lock
lock/acquired进行的-只要对队列的所有其他操作也受到保护,这里就没有问题。您的第一个忙等待示例——有一个线程重击锁,检查锁,然后释放锁。这既增加了互斥锁的争用,又增加了wast当没有处理任何内容时,它会占用整个CPU
第二个例子的等待线程大部分处于休眠状态。它只有在数据准备就绪或出现“虚假唤醒”(在标准允许的情况下)时才会唤醒
当它醒来时,它重新获取互斥体并检查谓词。如果谓词失败,它将释放锁并再次等待条件变量
这是安全的,因为谓词保证在您获取的互斥锁内运行,并传递给wait
函数。不是100%确定,但我认为传递给wait
的谓词仅在获取锁的情况下调用,因此在这种情况下是安全的(因为队列
上的每个操作都是用互斥锁
)锁定的。您的第二个版本是“安全的”……而且我知道第二个示例会降低CPU使用率。哦,天哪,多大的错误。锁定互斥锁后使用谓词。毫无疑问。没有必要那么清楚,因为。wait()
实际上释放了锁。但它不会在不持有锁的情况下检查状态。如果标准中真的提到这一点,那就太好了,不是吗?;-)
auto pred = [](){return !queue.empty();};
std::unique_lock<std::mutex> lock(mutex); // acquire
while (!pred) { // Ok, we are locked
condition_variable.wait(lock); // release
// if you get here, the lock as been re-acquired
}