C++ Boost group_线程最大并行线程数

C++ Boost group_线程最大并行线程数,c++,multithreading,boost,C++,Multithreading,Boost,我想在我的程序中使用最大数量的线程来应用boost group_线程。比如说 int maxNumberOfThreads boost::thread_group group; for (int i = 0; i < N; ++i) //create new if group.size() is smaller then maximal number of threads group.create_thread(Worker); group.join_all()

我想在我的程序中使用最大数量的线程来应用boost group_线程。比如说

int maxNumberOfThreads
boost::thread_group group;
 for (int i = 0; i < N; ++i)
      //create new if group.size() is smaller then maximal number of threads
      group.create_thread(Worker);
 group.join_all();
int-maxNumberOfThreads
boost::线程组;
对于(int i=0;i
有人知道我该如何实现这一点

因为当我启动N个线程时,效率会非常低


感谢您的帮助

您似乎想要的是一个线程池

您可以使用
boost::thread::hardware\u concurrency()
来确定特定系统上可用的(逻辑)内核数量

这是我上周为得到答案而推出的一款:

#include <boost/thread.hpp>
#include <boost/phoenix.hpp>
#include <boost/optional.hpp>

using namespace boost;
using namespace boost::phoenix::arg_names;

boost::atomic_size_t counter(0ul);

class thread_pool
{
  private:
      mutex mx;
      condition_variable cv;

      typedef function<void()> job_t;
      std::deque<job_t> _queue;

      thread_group pool;

      boost::atomic_bool shutdown;
      static void worker_thread(thread_pool& q)
      {
          while (optional<job_t> job = q.dequeue())
              (*job)();
      }

  public:
      thread_pool() : shutdown(false) {
          for (unsigned i = 0; i < boost::thread::hardware_concurrency(); ++i)
              pool.create_thread(bind(worker_thread, ref(*this)));
      }

      void enqueue(job_t job) 
      {
          lock_guard<mutex> lk(mx);
          _queue.push_back(job);

          cv.notify_one();
      }

      optional<job_t> dequeue() 
      {
          unique_lock<mutex> lk(mx);
          namespace phx = boost::phoenix;

          cv.wait(lk, phx::ref(shutdown) || !phx::empty(phx::ref(_queue)));

          if (_queue.empty())
              return none;

          job_t job = _queue.front();
          _queue.pop_front();

          return job;
      }

      ~thread_pool()
      {
          shutdown = true;
          {
              lock_guard<mutex> lk(mx);
              cv.notify_all();
          }

          pool.join_all();
      }
};
#包括
#包括
#包括
使用名称空间boost;
使用名称空间boost::phoenix::arg_名称;
boost::原子大小计数器(0ul);
类线程池
{
私人:
互斥mx;
条件变量cv;
类型定义功能作业;
std::deque_队列;
线程组池;
原子弹爆炸关机;
静态无效工作线程(线程池和q)
{
while(可选作业=q.dequeue())
(*工作)();
}
公众:
线程池():关闭(false){
for(无符号i=0;i
答案中还有一个典型的使用方法:

static const size_t bignumber = 1 << 20;

class myClass 
{
    thread_pool pool; // uses 1 thread per core

  public:
    void launch_jobs()
    {
        std::cout << "enqueuing jobs... " << std::flush;
        for(size_t i=0; i<bignumber; ++i)
        {
            for(int j=0; j<2; ++j) {
                pool.enqueue(bind(&myClass::myFunction, this, j, i));
            }     
        }
        std::cout << "done\n";
    }

  private:
    void myFunction(int i, int j)
    {
        boost::this_thread::sleep_for(boost::chrono::milliseconds(1));
        counter += 1;
    }
};

int main()
{
    myClass instance;
    instance.launch_jobs();

    size_t last = 0;
    while (counter < (2*bignumber))
    {
        boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
        if ((counter >> 4u) > last)
        {
            std::cout << "Progress: " << counter << "/" << (bignumber*2) << "\n";
            last = counter >> 4u;
        }
    }
}
static const size\t bignumber=1这是我的(不完善的)实现:

/**
 * \author Christophe Dumeunier
 * \brief  Extension of boost::thread_group managing a maximum number of threads running in parallel
 */
class thread_group_max : public boost::thread_group
{
    public:
        /**
         * \brief  Instanciate a group for threads
         * \param  max_running_threads  Maximum number of threads running in parallel, if 0 use the number of cores
         * \param    max_sleeping_time  Maximum sleeping time (seconds) between two checks for finished threads (must be > sleeping_time_start)
         * \param   sleeping_time_grow  Coefficient increasing sleeping time while waiting for finished threads (must be > 1)
         * \param  sleeping_time_start  Initial sleeping time (must be > 0)
         */
        explicit                   thread_group_max(std::size_t max_running_threads = 0, float max_sleeping_time = 1.0f,
                                                    float sleeping_time_grow = 1.1f, float sleeping_time_start = 0.001f);
        /**
         * \brief  Destroy the group
         * \note   Doesn't join the unterminated threads
         */
                                   ~thread_group_max();

        /** \brief Wait for an available slot and then create a new thread and launch it */
        template<typename F>
        boost::thread*             create_thread(F f);

    private:
        std::size_t                maxRunningThreads;  //!< Maximum number of running threads
        float                      maxSleepingTime;    //!< Maximum sleeping time between two checks for finished threads
        float                      sleepingTimeStart;  //!< Initial sleeping time
        float                      sleepingTimeGrow;   //!< Coefficient increasing sleeping time while waiting for finished threads
        std::set<boost::thread*>   runningThreads;     //!< Pointers to running or finished-but-not-removed-yet threads
};

thread_group_max::thread_group_max(std::size_t max_running_threads, float max_sleeping_time, float sleeping_time_grow, float sleeping_time_start) :
    boost::thread_group(),
    maxRunningThreads(max_running_threads == 0 ? std::max(boost::thread::hardware_concurrency(), 1u) : max_running_threads),
    maxSleepingTime(max_sleeping_time),
    sleepingTimeStart(sleeping_time_start),
    sleepingTimeGrow(sleeping_time_grow),
    runningThreads()
{
    assert(this->maxRunningThreads > 0);
    assert(this->maxSleepingTime >= this->sleepingTimeStart);
    assert(this->sleepingTimeStart > 0.0f);
    assert(this->sleepingTimeGrow > 1.0f);
}

thread_group_max::~thread_group_max()
{}

template<typename F>
boost::thread* thread_group_max::create_thread(F f)
{
    // First, try to clean already finished threads
    if(this->runningThreads.size() >= this->maxRunningThreads)
    {
        for(std::set<boost::thread*>::iterator it = this->runningThreads.begin(); it != this->runningThreads.end();)
        {
            const std::set<boost::thread*>::iterator jt = it++;
            if((*jt)->timed_join(boost::posix_time::milliseconds(0))) /// @todo timed_join is deprecated
                this->runningThreads.erase(jt);
        }
    }

    // If no finished thread found, wait for it
    if(this->runningThreads.size() >= this->maxRunningThreads)
    {
        float sleeping_time = this->sleepingTimeStart;
        do
        {
            boost::this_thread::sleep(boost::posix_time::milliseconds((long int)(1000.0f * sleeping_time)));
            for(std::set<boost::thread*>::iterator it = this->runningThreads.begin(); it != this->runningThreads.end();)
            {
                const std::set<boost::thread*>::iterator jt = it++;
                if((*jt)->timed_join(boost::posix_time::milliseconds(0))) /// @todo timed_join is deprecated
                    this->runningThreads.erase(jt);
            }
            if(sleeping_time < this->maxSleepingTime)
            {
                sleeping_time *= this->sleepingTimeGrow;
                if(sleeping_time > this->maxSleepingTime)
                    sleeping_time = this->maxSleepingTime;
            }
        } while(this->runningThreads.size() >= this->maxRunningThreads);
    }

    // Now, at least 1 slot is available, use it
    return *this->runningThreads.insert(this->boost::thread_group::create_thread(f)).first;
}
/**
*\作者Christophe Dumeunier
*\ boost::thread_组的简短扩展管理并行运行的最大线程数
*/
class thread\u group\u max:public boost::thread\u group
{
公众:
/**
*\brief实例化线程组
*\param max_running_threads并行运行的最大线程数,如果为0,则使用内核数
*\param max\u sleep\u time两次检查已完成线程之间的最大睡眠时间(秒)(必须>睡眠时间\u start)
*\param sleep\u time\u grow系数在等待完成的线程时增加睡眠时间(必须大于1)
*\param sleeping\u time\u start初始睡眠时间(必须大于0)
*/
显式线程组最大值(std::size\u t max\u running\u threads=0,float max\u sleeping\u time=1.0f,
浮动睡眠时间增长=1.1f,浮动睡眠时间开始=0.001f);
/**
*\brief销毁该组
*\note不连接未终止的线程
*/
~thread_group_max();
/**\请等待一个可用的插槽,然后创建一个新线程并启动它*/
模板
boost::thread*创建_线程(F);
私人:
std::size\u t maxRunningThreads;//!<运行线程的最大数量
float maxleepingtime;//!<两次检查已完成线程之间的最大休眠时间
浮动睡眠时间开始;//!<初始睡眠时间
float sleepingTimeGrow;//!<系数在等待完成的线程时增加睡眠时间
std::set runningThreads;//!<指向正在运行或已完成但尚未删除的线程的指针
};
线程组最大值::线程组最大值(标准::大小最大值运行线程、浮动最大值睡眠时间、浮动睡眠时间增长、浮动睡眠时间开始):
boost::线程组(),
maxRunningThreads(max\u running\u threads==0?std::max(boost::thread::hardware\u concurrency(),1u):max\u running\u threads),
最大睡眠时间(最大睡眠时间),
睡眠时间开始(睡眠时间开始),
睡眠时间增长(睡眠时间增长),
运行线程()
{
断言(此->maxRunningThreads>0);
断言(this->maxleepingtime>=this->sleepingTimeStart);
断言(此->sleepingTimeStart>0.0f);
断言(this->sleepingTimeGrow>1.0f);
}
thread_group_max::~thread_group_max()
{}
模板
boost::thread*thread\u group\u max::create\u thread(F)
{
//首先,尝试清理已经完成的线程
如果(this->runningThreads.size()>=this->maxRunningThreads)
{
对于(std::set::iterator it=this->runningThreads.begin();it!=this->runningThreads.end();)
{
常量std::set::迭代器jt=it++;
if((*jt)->timed_join(boost::posix_time::millides(0))//@todo timed_join不推荐使用
这->运行threads.erase(jt);
}
}
//如果没有找到完成的线程,请等待它
如果(this->runningThreads.size()>=this->maxRunningThreads)
{
浮动睡眠时间=此->睡眠时间开始;
做
{
boost::this_线程::sleep(boost::posix_时间::毫秒((long int)(1000.0f*睡眠时间));
对于(std::set::iterator it=this->runningThreads.begin();it!=this->runningThreads.end();)
{
常量std::set::迭代器jt=it++;
if((*jt)->timed_join(boost::posix_time::millides(0))//@todo timed_join不推荐使用
这->运行threads.erase(jt);
}
如果(
thread_group_max group(num_threads);
for(std::size_t i = 0; i < jobs.size(); ++i)
  group.create_thread(boost::bind(&my_run_job_function, boost::ref(job[i])));
group.join_all();