1, std:lock_guard、unique_lock、condition_variable 一般使用实例
#include <condition_variable>
#include <mutex>
#include <future>
#include <iostream>
bool readyFlag;
std::mutex readyMutex;
std::condition_variable readyCondVar;
void thread1()
{
// do something thread2 needs as preparation
std::cout << "<return>" << std::endl;
std::cin.get();
// signal that thread1 has prepared a condition
{
std::lock_guard<std::mutex> lg(readyMutex);
readyFlag = true;
} // release lock
readyCondVar.notify_one();
}
void thread2()
{
// wait until thread1 is ready (readyFlag is true)
{
std::unique_lock<std::mutex> ul(readyMutex);
readyCondVar.wait(ul, []{ return readyFlag; });
} // release lock
// do whatever shall happen after thread1 has prepared things
std::cout << "done" << std::endl;