简体   繁体   English

Boost asio无法在所有线程上运行io服务。io服务在第一次线程调用时被阻止

[英]Boost asio not able to run io service on all threads., io service is blocking at first thread call

I am trying to create a multi-threaded server by following the example HTTP Server 3 . 我试图通过遵循示例HTTP Server 3创建多线程服务器。 I have followed as shown in the example. 我按照示例中所示进行操作。

server code for creation of threads. 用于创建线程的服务器代码。 version 1 版本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();
  }

version 2 版本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();
  }

version 1 gives me output: 版本1给我输出:

performing join operation for thread = 0 对线程= 0执行联接操作

version 2 gives me the following output with 10 threads. 版本2为我提供了10个线程的以下输出。

[Tcp::Server::run@76] Initiating threads Initiating thread [Tcp :: Server :: run @ 76]启动线程启动线程
Initiating thread 启动线程
Initiating thread 启动线程
Initiating thread 启动线程
Initiating thread 启动线程
Initiating thread 启动线程
Initiating thread 启动线程
Initiating thread 启动线程
performing join operation for thread = 0 对线程= 0执行联接操作
Initiating thread 启动线程
Initiating thread 启动线程

I want to understand whether all threads are used, or only one thread that executed join operation is used. 我想了解是使用了所有线程,还是仅使用了执行联接操作的一个线程。 Can someone please explain where I went wrong and suggest if there is any good approach to do this, I am also using strand in the same way as example suggested. 有人可以解释我哪里出了问题,并建议是否有任何好的方法可以做到这一点,我也按照示例所建议的方式使用strand。

Thanks in advance 提前致谢

I want to understand wether all threads are used, or only one thread that executed join operation is used.. 我想了解是否使用了所有线程,或者仅使用了执行联接操作的一个线程。

The former. 前者。 The join function will just block the until the to be joined thread has exited execution, but generally all threads will immediately start to run once they got handed over a routine (eg by constructor) they can execute, irrespective of any join or detach calls. join函数将一直阻塞直到待加入的线程退出执行为止,但是通常所有线程一旦移交给它们可以执行的例程(例如,由构造函数),便会立即开始运行,而不管任何joindetach调用如何。

In context of asio of course only those threads can handle I/O that made a call to eg io_service::run . 当然,在asio上下文中,只有那些线程才能处理对io_service::run进行调用的I / O。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM