[英]Boost asio not able to run io service on all threads., io service is blocking at first thread call
我试图通过遵循示例HTTP Server 3创建多线程服务器。 我按照示例中所示进行操作。
用于创建线程的服务器代码。 版本1
for (std::size_t i = 0; i < thread_pool_size_; ++i) {
boost::shared_ptr<boost::thread> thread(new boost::thread(boost::bind(&boost::asio::io_service::run, &ios_)));
threads_.push_back(thread);
}
for (std::size_t i = 0; i < threads_.size(); ++i){
LOGV << "performing join operation for thread = "<< i;
threads_[i]->join();
}
版本2
for (std::size_t i = 0; i < thread_pool_size_; ++i) {
boost::shared_ptr<boost::thread> thread(new boost::thread(
[this](){ LOGV << "Intiating thread"; this->ios_.run(); }));
threads_.push_back(thread);
}
for (std::size_t i = 0; i < threads_.size(); ++i){
LOGV << "performing join operation for thread = "<< i;
threads_[i]->join();
}
版本1给我输出:
对线程= 0执行联接操作
版本2为我提供了10个线程的以下输出。
[Tcp :: Server :: run @ 76]启动线程启动线程
启动线程
启动线程
启动线程
启动线程
启动线程
启动线程
启动线程
对线程= 0执行联接操作
启动线程
启动线程
我想了解是使用了所有线程,还是仅使用了执行联接操作的一个线程。 有人可以解释我哪里出了问题,并建议是否有任何好的方法可以做到这一点,我也按照示例所建议的方式使用strand。
提前致谢
我想了解是否使用了所有线程,或者仅使用了执行联接操作的一个线程。
前者。 join
函数将一直阻塞直到待加入的线程退出执行为止,但是通常所有线程一旦移交给它们可以执行的例程(例如,由构造函数),便会立即开始运行,而不管任何join
或detach
调用如何。
当然,在asio上下文中,只有那些线程才能处理对io_service::run
进行调用的I / O。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.