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

后端 未结 4 941
梦如初夏
梦如初夏 2020-12-09 18:59

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_th         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-09 19:19

    I have something like this:

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

提交回复
热议问题