How to make boost::thread_group execute a fixed number of parallel threads How to make boost::thread_group execute a fixed number of parallel threads multithreading multithreading

How to make boost::thread_group execute a fixed number of parallel threads


Another, more efficient solution would be to have each thread callback to the primary thread when they are finished, and the handler on the primary thread could launch a new thread each time. This prevents the repetitive calls to timed_join, as the primary thread won't do anything until the callback is triggered.


I have something like this:

    boost::mutex mutex_;    boost::condition_variable condition_;    const size_t throttle_;    size_t size_;    bool wait_;    template <typename Env, class F>    void eval_(const Env &env, const F &f) {        {               boost::unique_lock<boost::mutex> lock(mutex_);            size_ = std::min(size_+1, throttle_);            while (throttle_ <= size_) condition_.wait(lock);        }        f.eval(env);        {            boost::lock_guard<boost::mutex> lock(mutex_);            --size_;         }        condition_.notify_one();    }


I think you are looking for a thread_pool implementation, which is available here.

Additionally I have noticed that if you create a vector of std::future and store futures of many std::async_tasks in it and you do not have any blocking code in the function passed to the thread, VS2013 (atleast from what I can confirm) will launch exactly the appropriate no of threads your machine can handle. It reuses the threads once created.