<legend id='phzoy'><style id='phzoy'><dir id='phzoy'><q id='phzoy'></q></dir></style></legend>

  • <tfoot id='phzoy'></tfoot>

      <small id='phzoy'></small><noframes id='phzoy'>

        • <bdo id='phzoy'></bdo><ul id='phzoy'></ul>
        <i id='phzoy'><tr id='phzoy'><dt id='phzoy'><q id='phzoy'><span id='phzoy'><b id='phzoy'><form id='phzoy'><ins id='phzoy'></ins><ul id='phzoy'></ul><sub id='phzoy'></sub></form><legend id='phzoy'></legend><bdo id='phzoy'><pre id='phzoy'><center id='phzoy'></center></pre></bdo></b><th id='phzoy'></th></span></q></dt></tr></i><div id='phzoy'><tfoot id='phzoy'></tfoot><dl id='phzoy'><fieldset id='phzoy'></fieldset></dl></div>
      1. Boost group_threads 最大并行线程数

        时间:2023-07-19

        1. <i id='mGZ8u'><tr id='mGZ8u'><dt id='mGZ8u'><q id='mGZ8u'><span id='mGZ8u'><b id='mGZ8u'><form id='mGZ8u'><ins id='mGZ8u'></ins><ul id='mGZ8u'></ul><sub id='mGZ8u'></sub></form><legend id='mGZ8u'></legend><bdo id='mGZ8u'><pre id='mGZ8u'><center id='mGZ8u'></center></pre></bdo></b><th id='mGZ8u'></th></span></q></dt></tr></i><div id='mGZ8u'><tfoot id='mGZ8u'></tfoot><dl id='mGZ8u'><fieldset id='mGZ8u'></fieldset></dl></div>
        2. <small id='mGZ8u'></small><noframes id='mGZ8u'>

                <bdo id='mGZ8u'></bdo><ul id='mGZ8u'></ul>

                  <tbody id='mGZ8u'></tbody>

              • <legend id='mGZ8u'><style id='mGZ8u'><dir id='mGZ8u'><q id='mGZ8u'></q></dir></style></legend>
                  <tfoot id='mGZ8u'></tfoot>
                  本文介绍了Boost group_threads 最大并行线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在我的程序中使用最大线程数应用 boost 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::thread::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
                  ";
                      }
                  
                    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) << "
                  ";
                              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:

                  • 提升线程抛出异常"thread_resource_error: 资源暂时不可用"

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

                  上一篇:权重图作为 Boost Graph Dijkstra 算法中的函数 下一篇:Boost.Spirit.Qi:动态创建“差异";解析时的解析器

                  相关文章

                  <i id='mYWml'><tr id='mYWml'><dt id='mYWml'><q id='mYWml'><span id='mYWml'><b id='mYWml'><form id='mYWml'><ins id='mYWml'></ins><ul id='mYWml'></ul><sub id='mYWml'></sub></form><legend id='mYWml'></legend><bdo id='mYWml'><pre id='mYWml'><center id='mYWml'></center></pre></bdo></b><th id='mYWml'></th></span></q></dt></tr></i><div id='mYWml'><tfoot id='mYWml'></tfoot><dl id='mYWml'><fieldset id='mYWml'></fieldset></dl></div>
                  <tfoot id='mYWml'></tfoot>

                  <small id='mYWml'></small><noframes id='mYWml'>

                1. <legend id='mYWml'><style id='mYWml'><dir id='mYWml'><q id='mYWml'></q></dir></style></legend>
                    • <bdo id='mYWml'></bdo><ul id='mYWml'></ul>