一只甜甜圈
我知道你喜欢代码。我的版本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; }}