condition
#include <deque> #include <thread> #include <mutex> #include <condition_variable> using namespace std; deque<int> queue; mutex mtx; condition_variable cond; void dequeue() { //1.必须与mutex一起使用 //2.mutex已上锁的时候才能调用wait() //3.把bool条件判断放到while中 unique_lock<mutex> lock(mtx); while(queue.empty()) { cond.wait(lock);//会解锁,并等待 //wait执行完毕自动重新加锁 } queue.pop_front(); } void enqueue() { unique_lock<mutex> lock(mtx); queue.push_back(1); cond.notify_one(); } //int main() //{ // thread th1(dequeue); // thread th2(enqueue); // // th1.join(); // th2.join(); // return 0; //}