17

Is it possible to create a thread pool using boost's thread? i was looking all over boost's libs and I couldn't find a thread pool manager (or something like that)... Is there a way to do it?

tnx!

grich
  • 463
  • 3
  • 6
  • 14
  • See also: http://stackoverflow.com/questions/19500404/how-to-create-a-thread-pool-using-boost-in-c – Jeroen Mar 11 '14 at 21:31
  • I posted a threadpool example using boost::asio here. [Thread Pool Example with boost::asio][1] [1]: http://stackoverflow.com/questions/31835009/c-threadpool-is-not-running-parallel/31835802#31835802 – DannyK Aug 05 '15 at 14:51
  • Possible duplicate of [How to create a thread pool using boost in C++?](https://stackoverflow.com/questions/19500404/how-to-create-a-thread-pool-using-boost-in-c) – Jeroen Nov 18 '17 at 14:34

2 Answers2

36

I know an answer has been accepted, if you need this right now, and you can't be bothered to write your own thread pool, you could try using boost asio io_service with a concurrency hint (i.e. how many threads it should run) and then post() stuff to this io_service... just an idea..

Nim
  • 33,299
  • 2
  • 62
  • 101
  • 4
    Using boost asio is the right way to go. Better not to manage the thread pool directly. Same way Intel Threading Building Blocks (TBB) does. – deepsnore Nov 03 '10 at 10:57
  • Can u add more about it? it sounds very interesting! – grich Nov 03 '10 at 16:46
  • I posted my thread pool example that is based on the Asio Recipes. http://stackoverflow.com/questions/31835009/c-threadpool-is-not-running-parallel/31835802#31835802 – DannyK Aug 10 '15 at 19:57
9

There is an unofficial (yet) threadpool in boost. But it's not a problem to implement one yourself especially if great genericity is not a primary goal. Idea: your threadpool can be parametrized with TaskType type and the number of workers. The TP must be given the handler function which takes TaskType. TP contains a queue of added tasks. The real thread function just takes a task from the queue and calls the passed handler. Something like that.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • I was hoping they already added the threadpool library.. Any idea if it is going to be added soon? thanks a lot for your quick answer! – grich Nov 03 '10 at 07:08
  • I don't know for sure but I am guessing and hoping that both ThreadPool and Process will be in 1.45 – Armen Tsirunyan Nov 03 '10 at 07:10
  • The threadpool library looked nice; too bad the documentation is lacking (many pages state TODO and warn that the description is out of date). Looks like it hasn't received much attention since 2008. – Edward Kirton Jul 15 '16 at 20:32