首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C++中使用boost创建线程池?

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

Stack Overflow用户
提问于 2013-10-22 00:55:38
回答 3查看 69.6K关注 0票数 62

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-22 00:55:38

这个过程非常简单。首先创建一个asio::io_service和一个thread_group。用链接到io_service的线程填充thread_group。使用boost::bind函数将任务分配给线程。

要停止线程(通常在退出程序时),只需停止io_service并加入所有线程即可。

您应该只需要以下标头:

代码语言:javascript
复制
#include <boost/asio/io_service.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>

下面是一个示例:

代码语言:javascript
复制
/*
 * 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();

来源:Recipes < Asio

票数 76
EN

Stack Overflow用户

发布于 2019-01-30 16:27:43

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

代码语言:javascript
复制
#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();

请参阅description

票数 38
EN

Stack Overflow用户

发布于 2014-10-21 14:07:16

我知道你喜欢编程。

My Version

代码语言:javascript
复制
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
    };
}

使用它的代码片段:

代码语言:javascript
复制
{
    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;
    }
}
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19500404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档