this blog私は、boost :: asioを使用して簡単なスレッドプールを作成する方法について非常にきちんとした例を見つけました。汎用タスクのためのboost :: asioスレッドプールの使用
#include <thread>
#include <functional>
#include <boost/asio.hpp>
int main (int argc, char* argv[]) {
asio::io_service io_service;
asio::io_service::work work(io_service);
std::vector<std::thread> threadPool;
for(size_t t = 0; t < std::thread::hardware_concurrency(); t++){
threadPool.push_back(thread(std::bind(&asio::io_service::run, &io_service)));
}
io_service.post(std::bind(an_expensive_calculation, 42));
io_service.post(std::bind(a_long_running_task, 123));
//Do some things with the main thread
io_service.stop();
for(std::thread& t : threadPool) {
t.join();
}
}
ブースト:: ASIOは、主にネットワークIOのために作られた、私の知る限りでは、次のとおりです。私は基本的にはこのようにそれを使用したいです。しかし、私は主に汎用関数に使用したいと思っています。並行性の問題は、asio::io_service::strand
を使用して解決されます。
私の質問:私のプログラムがネットワークIOを使用しない場合でも、このようなスレッドプールを作成することをお勧めしますか?他のスレッドプール実装と比較して明らかなパフォーマンス低下はありますか?もしそうなら、より良い実装もありますか?
boost :: thread boost boost :: thread_groupを使用すると、スレッドをグループ化することができます – StereoMatching