我有一个调度程序类(下面的剥离实现),它创建一个I / O服务并允许执行io_service
处理程序的启动工作线程。这些线程应保持活动状态,直到我告诉他们停止。为此我使用io_service::work
对象。但是,尽管存在工作对象,这些线程仍不会保持活动状态。关于如何正确使用io_service::work
的任何想法?
这是一个显示问题的最小工作示例(下面给出DispatcherClass
):
#include <boost/asio.hpp>
#include <boost/thread.hpp>
void myDispatcherMainThread(DispatcherClass &dispatcher)
{
std::cout << "Dispatcher Started\n";
dispatcher.AddCtrlCHandling();
if (dispatcher.Run(2))
std::cout << "Dispatcher is about to finish (expected).\n";
else
std::cout << "Dispatcher is about to finish (unexpected).\n";
}
int main(int argc, char* argv[]) {
DispatcherClass dispatcher;
boost::thread maindispatcherthread(myDispatcherMainThread,boost::ref(dispatcher));
boost::this_thread::sleep(boost::posix_time::seconds(5));
if (dispatcher.get_io_service().stopped())
std::cout << "Dispatcher should not have finished!";
dispatcher.cleanStop();
maindispatcherthread.join();
}
这是调度程序类(放在这里使这篇文章可读):
class DispatcherClass : private boost::noncopyable {
public:
DispatcherClass():
_ioService(),
_pKeepWorking(new boost::asio::io_service::work( _ioService )),
_expectingdispatchend(false){}
boost::asio::io_service &get_io_service() { return _ioService; }
// Start the dispatcher threads
bool Run(unsigned int numThreads = -1)
{
// Recover after a clean stop
if (!_pKeepWorking)
_pKeepWorking.reset(new boost::asio::io_service::work( _ioService ));
// Reset after clean or forced stop
if (_ioService.stopped())
{
_expectingdispatchend = false;
_ioService.reset();
}
// Start message dispatch threads
boost::thread_group workerThreads;
for (unsigned int i = 0; i < ((numThreads == (unsigned int)-1) ? (boost::thread::hardware_concurrency()) : numThreads); ++i)
workerThreads.create_thread(boost::bind(&DispatcherClass::WorkerThread, this));
// Wait until all threads finish
workerThreads.join_all();
return _expectingdispatchend;
}
// Stop the dispatcher threads
void Stop()
{
_expectingdispatchend = true;
_ioService.stop();
}
void cleanStop()
{
_expectingdispatchend = true;
_pKeepWorking.reset();
}
// Stop the dispatcher threads on CTRL-C
void AddCtrlCHandling()
{
boost::asio::signal_set sig_set(_ioService, SIGTERM, SIGINT);
sig_set.async_wait(boost::bind(&boost::asio::io_service::stop, boost::ref(_ioService)));
}
private:
bool _expectingdispatchend;
boost::asio::io_service _ioService;
boost::shared_ptr< boost::asio::io_service::work > _pKeepWorking;
void WorkerThread()
{
while (true) {
try
{
boost::system::error_code ec;
_ioService.run(ec);
break;
}
catch (const std::exception &) {}
}
}
};
答案 0 :(得分:2)
在取消的SignalHandler
中调用了io_service::stop()
,导致io_service::run()
的所有调用都尽快返回。
void DispatcherClass::AddCtrlCHandling()
{
boost::asio::signal_set sig_set(...);
sig_set.async_wait(boost::bind(
&boost::asio::io_service::stop, boost::ref(_ioService)));
}
在DispatcherClass::AddCtrlCHandling()
中,sig_set
是一个自动变量,其类型为boost::asio::signal_set
,其生命周期在函数返回后结束。在signal_set
析构函数中,signal_set
上的所有未完成的异步等待操作都将完成,错误代码为boost::asio::error::operation_aborted
。因此,将调用SignalHandler
的{{1}}排队到io_service::stop()
并由其中一个工作线程调用。要解决此问题,请考虑将io_service
作为signal_set
的成员变量来延长sig_set
的生命周期。
答案 1 :(得分:0)
缩小范围:您的线程在启动后立即终止:
boost::system::error_code ec;
_ioService.run(ec); // <----- blocks until the work is complete
break;
在我看来,没有工作要做,所以run
几乎立即返回(显示system:0
的错误代码,这似乎没问题。)
我不熟悉asio的API,但我认为潜在的问题是在生成线程之前没有正确设置_ioService
。