<legend id='OdYaJ'><style id='OdYaJ'><dir id='OdYaJ'><q id='OdYaJ'></q></dir></style></legend>
  • <i id='OdYaJ'><tr id='OdYaJ'><dt id='OdYaJ'><q id='OdYaJ'><span id='OdYaJ'><b id='OdYaJ'><form id='OdYaJ'><ins id='OdYaJ'></ins><ul id='OdYaJ'></ul><sub id='OdYaJ'></sub></form><legend id='OdYaJ'></legend><bdo id='OdYaJ'><pre id='OdYaJ'><center id='OdYaJ'></center></pre></bdo></b><th id='OdYaJ'></th></span></q></dt></tr></i><div id='OdYaJ'><tfoot id='OdYaJ'></tfoot><dl id='OdYaJ'><fieldset id='OdYaJ'></fieldset></dl></div>
  • <small id='OdYaJ'></small><noframes id='OdYaJ'>

    <tfoot id='OdYaJ'></tfoot>

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

      1. 如何在 C++ 中使用 boost 创建线程池?

        How to create a thread pool using boost in C++?(如何在 C++ 中使用 boost 创建线程池?)

        <small id='3S6JC'></small><noframes id='3S6JC'>

        <i id='3S6JC'><tr id='3S6JC'><dt id='3S6JC'><q id='3S6JC'><span id='3S6JC'><b id='3S6JC'><form id='3S6JC'><ins id='3S6JC'></ins><ul id='3S6JC'></ul><sub id='3S6JC'></sub></form><legend id='3S6JC'></legend><bdo id='3S6JC'><pre id='3S6JC'><center id='3S6JC'></center></pre></bdo></b><th id='3S6JC'></th></span></q></dt></tr></i><div id='3S6JC'><tfoot id='3S6JC'></tfoot><dl id='3S6JC'><fieldset id='3S6JC'></fieldset></dl></div>
          <tbody id='3S6JC'></tbody>
        1. <legend id='3S6JC'><style id='3S6JC'><dir id='3S6JC'><q id='3S6JC'></q></dir></style></legend>
            • <bdo id='3S6JC'></bdo><ul id='3S6JC'></ul>

              <tfoot id='3S6JC'></tfoot>

                • 本文介绍了如何在 C++ 中使用 boost 创建线程池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何在C++中使用boost创建线程池,以及如何将任务分配给线程池?

                  How do I create a thread pool using boost in C++, and how do I assign tasks to the threadpool?

                  推荐答案

                  过程非常简单.首先创建一个 asio::io_service 和一个 thread_group.用链接到 io_service 的线程填充 thread_group.使用 boost::bind功能.

                  The process is pretty simple. First create an asio::io_service and a thread_group. Fill the thread_group with threads linked to the io_service. Assign tasks to the threads using the boost::bind function.

                  要停止线程(通常在您退出程序时)只需停止 io_service 并加入所有线程.

                  To stop the threads (usually when you are exiting your program) just stop the io_service and join all threads.

                  您应该只需要这些标题:

                  You should only need these headers:

                  #include <boost/asio/io_service.hpp>
                  #include <boost/bind.hpp>
                  #include <boost/thread/thread.hpp>
                  

                  这是一个例子:

                  /*
                   * Create an asio::io_service and a thread_group (through pool in essence)
                   */
                  boost::asio::io_service ioService;
                  boost::thread_group threadpool;
                  
                  
                  /*
                   * This will start the ioService processing loop. All tasks 
                   * assigned with ioService.post() will start executing. 
                   */
                  boost::asio::io_service::work work(ioService);
                  
                  /*
                   * This will add 2 threads to the thread pool. (You could just put it in a for loop)
                   */
                  threadpool.create_thread(
                      boost::bind(&boost::asio::io_service::run, &ioService)
                  );
                  threadpool.create_thread(
                      boost::bind(&boost::asio::io_service::run, &ioService)
                  );
                  
                  /*
                   * This will assign tasks to the thread pool. 
                   * More about boost::bind: "http://www.boost.org/doc/libs/1_54_0/libs/bind/bind.html#with_functions"
                   */
                  ioService.post(boost::bind(myTask, "Hello World!"));
                  ioService.post(boost::bind(clearCache, "./cache"));
                  ioService.post(boost::bind(getSocialUpdates, "twitter,gmail,facebook,tumblr,reddit"));
                  
                  /*
                   * This will stop the ioService processing loop. Any tasks
                   * you add behind this point will not execute.
                  */
                  ioService.stop();
                  
                  /*
                   * Will wait till all the threads in the thread pool are finished with 
                   * their assigned tasks and 'join' them. Just assume the threads inside
                   * the threadpool will be destroyed by this method.
                   */
                  threadpool.join_all();
                  

                  来源:Recipes <亚欧

                  这篇关于如何在 C++ 中使用 boost 创建线程池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                • <tfoot id='uzVXU'></tfoot>

                      <tbody id='uzVXU'></tbody>

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

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

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