C++でboostを使用してスレッドプールを作成するにはどうすればいいですか?スレッドプールにタスクを割り当てるにはどうすればいいですか?C++でboostを使ってスレッドプールを作成する方法は?
答えて
このプロセスは非常に簡単です。まず、asio :: io_serviceとthread_groupを作成します。 io_serviceにリンクされたスレッドでthread_groupを埋めます。 boost::bind機能を使用してスレッドにタスクを割り当てます。
スレッドを停止するには(通常はプログラムを終了するとき)、io_serviceを停止してすべてのスレッドを結合します。あなただけのこれらのヘッダを必要とすべきである
:
#include <boost/asio/io_service.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
ここでは一例です:
/*
* Create an asio::io_service and a thread_group (through pool in essence)
*/
boost::asio::io_service ioService;
boost::thread_group threadpool;
/*
* This will start the ioService processing loop. All tasks
* assigned with ioService.post() will start executing.
*/
boost::asio::io_service::work work(ioService);
/*
* This will add 2 threads to the thread pool. (You could just put it in a for loop)
*/
threadpool.create_thread(
boost::bind(&boost::asio::io_service::run, &ioService)
);
threadpool.create_thread(
boost::bind(&boost::asio::io_service::run, &ioService)
);
/*
* This will assign tasks to the thread pool.
* More about boost::bind: "http://www.boost.org/doc/libs/1_54_0/libs/bind/bind.html#with_functions"
*/
ioService.post(boost::bind(myTask, "Hello World!"));
ioService.post(boost::bind(clearCache, "./cache"));
ioService.post(boost::bind(getSocialUpdates, "twitter,gmail,facebook,tumblr,reddit"));
/*
* This will stop the ioService processing loop. Any tasks
* you add behind this point will not execute.
*/
ioService.stop();
/*
* Will wait till all the threads in the thread pool are finished with
* their assigned tasks and 'join' them. Just assume the threads inside
* the threadpool will be destroyed by this method.
*/
threadpool.join_all();
['boost :: asio :: io_service :: work']オブジェクト(http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/io_service__work.html)オブジェクトは重要な部分ですこれを適切に機能させるにはまた、['io_service :: stop()'](http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/io_service/stop.html)は、追加のタスクの実行を防ぎますが、タスクが 'io_service'にいつ投稿されるかは関係ありません。たとえば、 'getSocialUpdates()'は 'stop()'の前に 'io_service'キューに追加されますが、' stop() 'が呼び出されたときに実行中でなければ、キューに入れられます。 –
ワーカースレッドを作成する前に 'work'オブジェクト*を作成する必要があります。そうしないと、何もせずにただちに終了することがあります。 – Miral
@TannerSansbury io_service.stop()の後で私の未完成の仕事がすべて殺されるので、このレシピは私を非常に混乱させます。適切な方法は、ioservice.stop()を削除するが、作業オブジェクトを破棄してから、すべてのジョブを終了させるためにthreadpool.join_all()を呼び出すことです。 – CyberSnoopy
私はあなたがコードを好きな知っています。
マイバージョン
namespace bamthread
{
typedef std::unique_ptr<boost::asio::io_service::work> asio_worker;
struct ThreadPool {
ThreadPool(size_t threads) :service(), working(new asio_worker::element_type(service)) {
while(threads--)
{
auto worker = boost::bind(&boost::asio::io_service::run, &(this->service));
g.add_thread(new boost::thread(worker));
}
}
template<class F>
void enqueue(F f){
service.post(f);
}
~ThreadPool() {
working.reset(); //allow run() to exit
g.join_all();
service.stop();
}
private:
boost::asio::io_service service; //< the io_service we are wrapping
asio_worker working;
boost::thread_group g; //< need to keep track of threads so we can join them
};
}
それを使用するコードの一部:[ブーストを使用してスレッドプールを作成する]の
{
bamthread::ThreadPool tp(n_threads);
BOOST_FOREACH(int y, boost::irange(starty, endy, step)){
int im_x = 0;
BOOST_FOREACH(int x, boost::irange(startx, endx, step)){
tp.enqueue (boost::bind(&camera_view_depth::threaded_intersection, this,
intersections,
intersected,
im_x,
im_y,
_faces, x, y));
++im_x;
}
++im_y;
}
}
可能重複(http://stackoverflow.com/質問/ 4084777 /スレッドプールを使用した作成) –
私は他の質問に答えることができず、自己回答は許可され、奨励されます。 –
あなたは[その他の質問](http://stackoverflow.com/questions/4084777/creating-a-thread-pool-using-boost)への回答を投稿できる必要があります。閉じられていないか、[保護されていません] (http://meta.stackexchange.com/questions/52764/what-is-a-protected-question)。 –