std::condition_variable 条件变量
void wait( std::unique_lock<std::mutex>& lock ); #1
template< class Predicate >
void wait( std::unique_lock<std::mutex>& lock, Predicate stop_waiting ); #2
例1 :
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
std::mutex m;
std::condition_variable cv;
std::string data;
bool ready = false;
bool processed = false;
void worker_thread()
{
std::unique_lock lk(m);
cv.wait(lk, [] { return ready; });
std::cout << "Worker thread is processing data\n";
data += " after processing";
processed = true;
std::cout << "Worker thread signals data processing completed\n";
lk.unlock();
cv.notify_one();
}
int main()
{
std::thread worker(worker_thread);
data = "Example data";
{
std::lock_guard lk(m);
ready = true;
std::cout << "main() signals data ready for processing\n";
}
cv.notify_one();
{
std::unique_lock lk(m);
cv.wait(lk, [] { return processed; });
}
std::cout << "Back in main(), data = " << data << '\n';
worker.join();
}
标签:std,lock,void,variable,include,condition From: https://www.cnblogs.com/sunbines/p/16596675.html