我正在尝试“C++编程语言”第4版中的示例,特别是对如何使用condition_variable的描述。代码片段如下:
class Message { // object to be communicated
// ...
};
queue<Message> mqueue; // the queue of messages
condition_variable mcond; // the variable communicating events
mutex mmutex; // the locking mechanism
void con
假设有三个线程A、B和C在某个点挂起,等待A发出继续的信号。在标准C++提供的线程同步工具中,std::condition_variable似乎最适合这里(尽管仍然不好)。由于std::condition_variable必须与锁一起使用,所以B和C的代码可能包含如下行:
{
std::mutex mut;
std::unique_lock<std::mutex> lock(mut);
cond_var.wait(lock); // cond_var is a global variable of type std::condition_variable`
}
注意,