升压group_threads并行线程的最大数目 [英] Boost group_threads Maximal number of parallel thread

查看:208
本文介绍了升压group_threads并行线程的最大数目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想申请提振group_thread在我的程序与线程的最大数量。例如:

i want to apply boost group_thread in my program with a maximal number of Threads. For example

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

有人有一个想法我怎么能实现呢?

Someone has an idea how i can realize this ?

由于这将是非常低效的,当我启动线程的N个。

Because it will be very inefficient when i start N numbers of thread.

感谢您的帮助。

推荐答案

您似乎什么要的是一个线程池。

What you seem to want is a thread pool.

您可以使用的boost ::螺纹:: hardware_concurrency()来确定您的特定系统上可用的(逻辑)内核的数量。

You can use boost::thread::hardware_concurrency() to determine the number of (logical) cores available on your particular system.

这里有一个我滚了上周的答案:

Here's one I rolled for an answer last week:

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

要使用的典型方法也是这个问题的答案:

A typical way to use that is also in that answer:

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

有关的奖金,在这个问题,在评论到另一个答案,我也贴基于同等解决方案的无锁的作业队列实现:

For bonus, at that question, in the comments to another answer, I also posted an equivalent solution based on a lock-free job queue implementation:

  • boost thread throwing exception "thread_resource_error: resource temporarily unavailable"

这篇关于升压group_threads并行线程的最大数目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆