如何用c语言创建线程池,如何在C ++中使用boost创建线程池?

c35af7fa1ca52c2e3a11e704f27d1635.png

一只甜甜圈

我知道你喜欢代码。我的版本namespace bamthread{    typedef std::unique_ptr<: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            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;    }}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值