boost条件有问题,cpp类中有这两个实例变量:
boost::condition wait_answer_condition;
boost::mutex wait_answer_mutex;然后,我有一个发送消息的方法,条件是:
method1
boost::unique_lock<boost::mutex> lock(wait_answer_mutex)
//do some work and send message
//wait the result
wait_answer_condition.wait(lk);
//get the result
result = responseIdSyncMap[currentRequestID];然后,我有另一个接收结果并唤醒发送线程的方法。
method2
int requestID = responseData->getInt32Value("response_id");
responseIdSyncMap.insert(make_pair(requestID, responseData));
wait_answer_condition.notify_one();这两个方法在不同的线程中调用。问题是,当调用method2时,wait_answer_condition在调用"wait_answer_condition.notify_one()“之前发布,而method1在未找到结果的情况下被唤醒。
有人知道这个吗?
发布于 2012-01-27 13:07:51
条件变量可能会被伪造地唤醒,而唤醒事件通常不会被存储(也就是说,在任何人等待它之前发出的唤醒就会丢失,就像在没有人听到的情况下,它会在树林中拍手)。因此,条件变量几乎总是在循环中使用:
bool is_answered = false;
// method1
boost::lock_guard<boost::mutex> lock( wait_answer_mutex );
while ( ! is_answered )
wait_answer_condition.wait(lock);
// method2
boost::lock_guard<boost::mutex> lock( wait_answer_mutex );
is_answered = true;
wait_answer_condition.notify_one();https://stackoverflow.com/questions/9033551
复制相似问题