13

这是创建 thread_group 并并行执行所有线程的代码:

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

此代码将一次执行所有线程。我想要做的是将它们全部并行执行,但最多并行执行 4 个。当 on 终止时,将执行另一个,直到没有更多要执行。

4

4 回答 4

3

另一种更有效的解决方案是让每个线程在完成时回调到主线程,并且主线程上的处理程序每​​次都可以启动一个新线程。这可以防止对 timed_join 的重复调用,因为在触发回调之前主线程不会做任何事情。

于 2010-07-27T14:29:49.627 回答
0

我有这样的事情:

    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();
    }
于 2010-07-27T15:24:40.027 回答
0

我认为您正在寻找一个thread_pool实现,可在此处获得。

另外我注意到,如果您创建一个 std::future 向量并在其中存储许多 std::async_tasks 的未来,并且您在传递给线程的函数中没有任何阻塞代码,VS2013(至少从我可以确认) 将完全启动您的机器可以处理的适当线程数。它重用创建后的线程。

于 2016-01-11T10:04:42.947 回答
0

我创建了自己的简化界面boost::thread_group来完成这项工作:

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

boost::thread_group这是一个使用示例,与线程的创建是一个等待点的主要区别非常相似:

{
  ThreadGroup group(4);
  for(int i = 0; i < 15; ++i)
    group.createThread(aFunctionToExecute);
} // join all at destruction
于 2017-10-25T15:34:31.797 回答