0

I'm developing a program in C++ for both POSIX-compatible systems and Windows(R) and was wondering:

  1. What is the best OS portable thread pool library? Or should I make my own?
  2. Is there any point of pooling more treads than there are physical processor cores?

5 Answers5

4
  1. The new C++11 standard has threading support, so if your compiler supports that, you should prefer it. Otherwise, there is Boost.Thread. Those aren't thread pool libraries, but you can build a thread pool on top of them.
  2. Depends on what problem you try to solve. If you want to run n tasks in parallel, you have to start n threads, no matter how many processor cores you have (multithreading was popular before processors grew multiple cores, and normally hundreds of threads are already running in parallel, so a few more don't matter that much). On the other hand, thread creation is expensive, so you shouldn't create a new thread for each short-lived background thread. In C++11, you can use futures for such background tasks.
Philipp
  • 48,066
  • 12
  • 84
  • 109
1
  1. If possible, the best thing is to use libraries which abstract threads away and instead focus on tasks, like Intel TBB/ Microsoft PPL. They also offer parallel algortihms like parallel_for which are easy to use and powerfull. They do in addition offer possibilities to solve loads of other different problems, like reduce operations, recursive stuff...

  2. In general, that is not good because excessive task switching between the threads and thread spwaning can decrease performance. Instead one should try - TBB and PPL do it - to split work on the different threads by applying a task-stealing pattern (see the TBB wiki page). The only place where a small oversubscription is good is when some threads have to wait and such would waste CPU processing time.

Stephan Dollberg
  • 32,985
  • 16
  • 81
  • 107
1

1) I'd go with Boost as well.

2) Maybe. If there is any locking or I/O blocking in the tasks, then it is possible that a large number of threads may be required. If the tasks are CPU-bound, it's more difficult to say. If the tasks don't read, and especially write, a lot of data and so don't invalidate that much cache when run, a large number of threads seems to actually improve performance slightly - if just adding into a variable, 200 threads get slightly more work done than 8. In the more common case of CPU-bound tasks that use a lot of memory and so tend to dirty all caches, a lot of threads, (eg. 200), typically results in a throughput drop of 20-50% because of cache flushing.

Martin James
  • 24,453
  • 3
  • 36
  • 60
1

There is a thread pool tool in dlib which allows you to specify the number of processing cores on your machine and then submit jobs to the thread pool. So if you are looking for something to make it easy to use a mutli-core CPU effectively then the dlib::thread_pool can be pretty useful.

There is also an example program which shows how to do things like submit jobs to a pool using a variety of methods, including C++11 lambda functions. Another nice thing is that there is no install process, you can just download dlib and this example program and compile it. You don't need to install or configure anything. It will just work on either of the platforms you mentioned.

Davis King
  • 4,731
  • 1
  • 25
  • 26
-2

Boost Threads are cross-platform and for C++: http://www.boost.org/doc/libs/1_49_0/doc/html/thread.html

j13r
  • 2,576
  • 2
  • 21
  • 28