boost async_wait返回处理程序永远不会被调用

时间:2016-07-13 15:51:54

标签: c++ boost boost-asio

我正在使用boost::asio tcp版本1.57,创建一个自定义服务器/客户端,大致遵循以下示例:Async_Tcp_Client,但我在其自己的线程中运行io_service run()服务器/客户端。此外,每个应用程序可以有多个服务器/客户端。

按照示例,当我不想发送消息时,我将await_output函数置于睡眠状态,当我想发送消息时,我将其唤醒(通过async_write)。经过不同程度的发送操作(有时少于10次,有时是几千次),我遇到了await_output截止日期(提升截止时间计时器)的奇怪行为。
在某些时候,针对计时器的async_wait只是“消失”,并且当我取消发送消息的截止日期时不会返回。

transmit函数,由拥有客户端/服务器的应用程序调用(但仅由应用程序调用,我猜它不是非常线程安全的);
正在等待await_output的{​​{1}}函数;
mOutputQueueDeadline函数:

handle_write

我尝试实施一种解决方法,当void SocketTcp::transmit(std::string pMsg) { if (mStopped) { return; } mOutputQueue.push(pMsg); // a global queue // Signal that the output queue contains messages. Modifying the expiry // will wake the output actor, if it is waiting on the timer. size_t quits = mOutputQueueDeadline.expires_at(boost::posix_time::neg_infin); //this returns '0' when the error occurs } void SocketTcp::await_output(const boost::system::error_code& ec) { if (mStopped) { return; } if (mOutputQueue.empty()) { size_t quits = mOutputQueueDeadline.expires_at(boost::posix_time::pos_infin); mOutputQueueDeadline.async_wait(boost::bind(&SocketTcp::await_output, this, _1)); //this async_wait starts a wait on the deadline, that sometimes never returns! } else { boost::asio::async_write(mSocket, boost::asio::buffer(mOutputQueue.front()), boost::bind(&SocketTcp::handle_write, this, _1)); } } void SocketTcp::handle_write(const boost::system::error_code& ec) { if (mStopped) { return; } if(!ec) { mOutputQueue.pop(); //remove sent element from queue boost::system::error_code errcode; await_output(errcode); //start the waiting actor for outgoing messages } else { mConnected = false; //update the connection status this->stop(); } } 返回0时重新启动await_output中的transmit(),但这导致两个演员在下次发送消息时被唤醒,然后运行陷入崩溃(expire_at - 设计不允许并行发送OP,更不用说尝试发送相同的消息......)

我尝试使用String iterator not dereferencable选项进行调试,并在此处发现错误:

BOOST_ASIO_ENABLE_HANDLER_TRACKING

我对c ++和stackoverflow都很陌生(尽管它已经多次安全了!)所以请告诉我是否能以某种方式改进我的问题!

0 个答案:

没有答案