[英]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.