Jeroen
Jeroen

Reputation: 16865

How to create a thread pool using boost in C++?

How do I create a thread pool using boost in C++, and how do I assign tasks to the threadpool?

Upvotes: 72

Views: 89289

Answers (4)

BullyWiiPlaza
BullyWiiPlaza

Reputation: 19223

Use the library BS::thread_pool. It's available on vcpkg, the library is widely used and very flexible:

#include <BS_thread_pool.hpp>

int main()
{
    BS::thread_pool pool(5);
    int squares[100];
    for (int i = 0; i < 100; ++i)
        pool.push_task(
            [&squares, i]
            {
                std::this_thread::sleep_for(std::chrono::milliseconds(50));
                squares[i] = i * i;
            });
    pool.wait_for_tasks();
    std::cout << squares[50];
}

Unlike Boost.Asio, this library is more lightweight and does not have symbol conflicts with Windows.h (e.g. see this question).

Upvotes: 1

Andrei
Andrei

Reputation: 904

Starting from boost 1.66.0, there is a thread_pool class:

#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();

See the description.

Upvotes: 63

Jeroen
Jeroen

Reputation: 16865

The process is pretty simple. First create an asio::io_service and a thread_group. Fill the thread_group with threads linked to the io_service. Assign tasks to the threads using the boost::bind function.

To stop the threads (usually when you are exiting your program) just stop the io_service and join all threads.

You should only need these headers:

#include <boost/asio/io_service.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>

here is an example:

/*
 * 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();

Source: Recipes < Asio

Upvotes: 76

squid
squid

Reputation: 2625

I know you like code.

My Version

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
    };
}

Piece of Code to Use It:

{
    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;
    }
}

Upvotes: 14

Related Questions