2013-10-21 10 views
38

C++에서 boost를 사용하여 스레드 풀을 만드는 방법과 스레드 풀에 작업을 할당하는 방법은 무엇입니까?C++에서 부스트를 사용하여 스레드 풀을 만드는 방법은 무엇입니까?

+0

([부스트를 사용하여 스레드 풀을 만들기]의 중복 가능성 http://stackoverflow.com/ 질문/4084777/Creating-a-thread-pool-using-boost) –

+0

오직 다른 질문에 답하는 것을 허용하지 않으며 자체 응답이 허용되고 권장됩니다. –

+0

[다른 질문] (http://stackoverflow.com/questions/4084777/creating-a-thread-pool-using-boost)에 대한 답변을 게시 할 수 있어야하며 닫히지 않았거나 [보호됨] (http://meta.stackexchange.com/questions/52764/what-is-a-protected-question). –

답변

59

과정이 매우 간단합니다. 먼저 asio :: io_service 및 thread_group을 만듭니다. io_service에 연결된 스레드로 thread_group을 채 웁니다. boost::bind 기능을 사용하여 스레드에 작업을 할당하십시오.

스레드를 중지하려면 (일반적으로 프로그램을 종료 할 때) io_service를 중지하고 모든 스레드를 결합하십시오.

당신은 이러한 헤더를 필요로한다 : 여기

#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 < Asio

+12

['boost :: asio :: io_service :: work'] (http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/io_service__work.html) 객체는 중요한 부분입니다 이것이 제대로 작동하려면. 또한 ['io_service :: stop()'] (http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/io_service/stop.html)은 추가 작업을 실행하지 못하도록합니다. 태스크가'io_service'에 언제 게시되는지에 관계없이. 예를 들어,'getSocialUpdates()'는'stop()'전에'io_service' 큐에 추가되는 동안,'stop()'이 호출 될 때 중간 실행이 아니면 대기열에 남아 있습니다. –

+2

작업자 스레드를 생성하기 전에'work' 객체 *를 생성해야합니다. 그렇지 않으면 아무것도 수행하지 않고 즉시 종료 될 수 있습니다. – Miral

+6

@TannerSansbury 실제로이 레서피는 io_service.stop() 이후 미완성 된 모든 작업이 종료되므로 매우 혼란 스럽습니다. 적절한 방법은 ioservice.stop()을 제거하고 작업 객체를 소멸시킨 다음 threadpool.join_all()을 호출하여 모든 작업이 완료되도록해야합니다. – CyberSnoopy

10

을 당신이 코드를 좋아하는 것을 알고있다. 코드의

내 버전

namespace bamthread 
{ 
    typedef std::unique_ptr<boost::asio::io_service::work> asio_worker; 

    struct ThreadPool { 
     ThreadPool(size_t threads) :service(), working(new asio_worker::element_type(service)) { 
      while(threads--) 
      { 
       auto worker = boost::bind(&boost::asio::io_service::run, &(this->service)); 
       g.add_thread(new boost::thread(worker)); 
      } 
     } 

     template<class F> 
      void enqueue(F f){ 
       service.post(f); 
      } 

     ~ThreadPool() { 
      working.reset(); //allow run() to exit 
      g.join_all(); 
      service.stop(); 
     } 

     private: 
     boost::asio::io_service service; //< the io_service we are wrapping 
     asio_worker working; 
     boost::thread_group g; //< need to keep track of threads so we can join them 
    }; 
} 

조각이 기능을 사용하려면

{ 
    bamthread::ThreadPool tp(n_threads); 
    BOOST_FOREACH(int y, boost::irange(starty, endy, step)){ 
     int im_x = 0; 
     BOOST_FOREACH(int x, boost::irange(startx, endx, step)){ 
      tp.enqueue (boost::bind(&camera_view_depth::threaded_intersection, this, 
         intersections, 
         intersected, 
         im_x, 
         im_y, 
         _faces, x, y)); 
      ++im_x; 
     } 
     ++im_y; 
    } 
} 
+17

죄송합니다. 물어볼 필요가 있습니다. 질문자가 코드를 좋아한다는 것을 어떻게 알 수 있습니까? – x29a

+11

@ x29a 질문자가 코드를 좋아한다는 것을 어떻게 알 수 있습니까? – squid

+20

당신이 내 주석에서 읽는 방법은 묻는 사람이 코드를 좋아하는지 모르겠다는 것을 알고 있습니까? – x29a