我已经提升了websocket服务器,它有时会陷入僵局,但很少发生,我不确定它会在哪种情况下发生。
代码如下:
mutex m_action_lock;
condition_variable m_action_cond;
queue<pair<websocketpp::connection_hdl, server::message_ptr>> m_actions;
void Gateway::on_message(websocketpp::connection_hdl hdl, message_ptr msg)
{
logMesaage("Incoming message...", "");
unique_lock<mutex> lock(m_action_lock);
m_actions.push(pair<websocketpp::connection_hdl, server::message_ptr> (hdl,msg));
lock.unlock();
m_action_cond.notify_one();
}
void Gateway::process_messages()
{
logMesaage("Start processing messages", "");
while (1)
{
unique_lock<mutex> lock(m_action_lock);
while (m_actions.empty())
{
m_action_cond.wait(lock);
}
pair<websocketpp::connection_hdl, server::message_ptr> action = m_actions.front();
m_actions.pop();
lock.unlock();
logMesaage("Received message", "");
}
logMesaage("End processing messages", "");
}
记录消息的正常流程应为:
Incoming message...
Received message
但有时它会停止工作,在我发送任何消息后,我只会记录下来:
Incoming message...
但是我在这段代码中没有看到任何死锁的地方吗? 感谢