boost::asio 阻塞卡顿问题
在windows使用boost库 1.70.x 版本, 使用相关的asio功能, 遇到了ioservice 无法退出的情况, 经调试卡顿在 win_iocp_io_context::shutdown()
函数无法退出, 后修改了此函数
while (::InterlockedExchangeAdd(&outstanding_work_, 0) > 0)
{
op_queue<win_iocp_operation> ops;
timer_queues_.get_all_timers(ops);
ops.push(completed_ops_);
if (!ops.empty())
{
while (win_iocp_operation* op = ops.front())
{
ops.pop();
::InterlockedDecrement(&outstanding_work_);
op->destroy();
}
}
else
{
DWORD bytes_transferred = 0;
dword_ptr_t completion_key = 0;
LPOVERLAPPED overlapped = 0;
::GetQueuedCompletionStatus(iocp_.handle, &bytes_transferred,
&completion_key, &overlapped, gqcs_timeout_);
if (overlapped)
{
::InterlockedDecrement(&outstanding_work_);
static_cast<win_iocp_operation*>(overlapped)->destroy();
}
else { break; } // fix: add new code for quite !
}
在使用 boost::asio::simple_queue 构建队列, 使用 boost::asio::queue_sender, boost::asio::queue_listener 中,
也遇到 队列不能退出, block 阻塞问题, 主要是service 中还挂载着异步io请求, 要释放这个请求。 解决的方案是:
msg
退出message
_message_queue_receiver->async_enq(msg,
boost::bind(&class_xxxx::on_receiver_default<boost::shared_ptr>, this));
在消息处理函数中 收到此消息 msg
时, 不在发起异步io请求调用, 即不再调用:
_message_queue_poster->async_deq(boost::bind(
&class_xxxx::pickup_message, this, err, boost::placeholders::_2));