如何在C++中使用boost创建线程池?

Jer*_*oen 51 c++ boost boost-thread boost-asio threadpool

如何使用C++中的boost创建线程池,如何将任务分配给线程池?

Jer*_*oen 68

这个过程非常简单.首先创建一个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>
Run Code Online (Sandbox Code Playgroud)

这是一个例子:

/*
 * 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();
Run Code Online (Sandbox Code Playgroud)

来源:食谱<Asio

  • [`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`.例如,虽然在`stop()`之前将`getSocialUpdates()`添加到`io_service`队列,但如果在调用`stop()`时它不是执行中,那么它将保持排队. (13认同)
  • @TannerSansbury实际上这个配方让我非常困惑,因为在io_service.stop()之后我所有未完成的工作都被杀死了.一个正确的方法应该是删除ioservice.stop()但是破坏工作对象,然后调用threadpool.join_all()让所有作业完成. (7认同)
  • 请参阅[io_service文档中的“停止io_service停止工作”](http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/io_service.html#boost_asio.reference。 io_service.stopping_the_io_service_from_running_out_of_work)关于io_service :: stop()(丢弃排队的工作)与销毁`work`对象(排空的工作)之间的区别。 (2认同)

squ*_*uid 12

我知道你喜欢代码.

我的版本

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
    };
}
Run Code Online (Sandbox Code Playgroud)

一块代码使用它:

{
    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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 对不起,我只想问,你怎么知道提问者喜欢代码? (28认同)
  • 你怎么读我的评论,我知道你不知道提问者是否喜欢代码? (28认同)
  • @ x29a和squid:注意无限递归.它会快速溢出stackoverflow的堆栈! (22认同)
  • @ x29a你怎么知道我不知道提问者喜欢的代码? (19认同)
  • 请!stackoverflow 的栈溢出!! (2认同)

And*_*rei 11

从boost 1.66.0开始,有一个thread_pool类:

#include <boost/asio/thread_pool.hpp>
#include <boost/asio/post.hpp>

boost::asio::thread_pool pool(4); // 4 threads
boost::asio::post(pool, [] {});
pool.join();
Run Code Online (Sandbox Code Playgroud)

请参阅说明