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

Viewed 7149

This is the code to create a thread_group and execute all threads in parallel:

boost::thread_group group;
for (int i = 0; i < 15; ++i)
    group.create_thread(aFunctionToExecute);
group.join_all();

This code will execute all threads at once. What I want to do is to execute them all but 4 maximum in parallel. When on is terminated, another one is executed until there are no more to execute.

4 Answers

I created my own simplified interface of boost::thread_group to do this job:

class ThreadGroup : public boost::noncopyable
{
    private:
        boost::thread_group        group;
        std::size_t                maxSize;
        float                      sleepStart;
        float                      sleepCoef;
        float                      sleepMax;
        std::set<boost::thread*>   running;

    public:
        ThreadGroup(std::size_t max_size = 0,
                    float max_sleeping_time = 1.0f,
                    float sleeping_time_coef = 1.5f,
                    float sleeping_time_start = 0.001f) :
            boost::noncopyable(),
            group(),
            maxSize(max_size),
            sleepStart(sleeping_time_start),
            sleepCoef(sleeping_time_coef),
            sleepMax(max_sleeping_time),
            running()
        {
            if(max_size == 0)
                this->maxSize = (std::size_t)std::max(boost::thread::hardware_concurrency(), 1u);
            assert(max_sleeping_time >= sleeping_time_start);
            assert(sleeping_time_start > 0.0f);
            assert(sleeping_time_coef > 1.0f);
        }

        ~ThreadGroup()
        {
            this->joinAll();
        }

        template<typename F> boost::thread* createThread(F f)
        {
            float sleeping_time = this->sleepStart;
            while(this->running.size() >= this->maxSize)
            {
                for(std::set<boost::thread*>::iterator it = running.begin(); it != running.end();)
                {
                    const std::set<boost::thread*>::iterator jt = it++;
                    if((*jt)->timed_join(boost::posix_time::milliseconds((long int)(1000.0f * sleeping_time))))
                        running.erase(jt);
                }
                if(sleeping_time < this->sleepMax)
                {
                    sleeping_time *= this->sleepCoef;
                    if(sleeping_time > this->sleepMax)
                        sleeping_time = this->sleepMax;
                }
            }
            return *this->running.insert(this->group.create_thread(f)).first;
        }

        void joinAll()
        {
            this->group.join_all();
        }

        void interruptAll()
        {
#ifdef BOOST_THREAD_PROVIDES_INTERRUPTIONS
            this->group.interrupt_all();
#endif
        }

        std::size_t size() const
        {
            return this->group.size();
        }
    };

Here is an example of use, very similar to boost::thread_group with the main difference that the creation of the thread is a waiting point:

{
  ThreadGroup group(4);
  for(int i = 0; i < 15; ++i)
    group.createThread(aFunctionToExecute);
} // join all at destruction
Related