I am working a c++ hobby project that requires lots of processing several times a second. Splitting up my work into multiple threads could improve the completion speed. When the Threads are done should I keep the Threads until I have more work for them or should I throw the threads away and just make new ones when I need them again?
-
1Maybe you just want: [https://en.cppreference.com/w/cpp/thread/async](https://en.cppreference.com/w/cpp/thread/async) – drescherjm Oct 05 '19 at 12:50
-
Really that's a design consideration you'll have to think about. If you're comfortable with creating/disposing of threads, you can achieve it either way. – J. Murray Oct 05 '19 at 12:51
-
2It all depends, on the type of work you want the threads to do. As it stands question is Too Broad as it does not include enough detail for a meaningful answer other than try it and see (eg IO bound tasks threads should be faster; memory/CPU bound even 2 threads could be slower than a single thread). Background reading "thread pool". – Richard Critten Oct 05 '19 at 12:52
-
3If you need to use them constantly then keep them around. “Several times a second” sounds like that. Benchmarking helps to know if you need to build a way to keep them around or can just create new when needed. – Sami Kuhmonen Oct 05 '19 at 12:52
-
1You'd strongly consider KISS first. Creating a threadpool in C++ is the subject of [this Q+A](https://stackoverflow.com/questions/15752659/thread-pooling-in-c11). – Hans Passant Oct 05 '19 at 12:54
2 Answers
If it's just several times a second (e.g. 10 times a second) then keep it simple and throw the thread away when it's done.
When you get to hundreds or thousands of threads, then you should start thinking about a thread pool.
All that is assuming you're working on a typical machine and not a weak CPU like a microcontroller.

- 5,528
- 1
- 7
- 15
When the Threads are done should I keep the Threads until I have more work for them or should I throw the threads away and just make new ones when I need them again?
It makes little sense paying for thread creation many times if you can pay the price just once (Greta would tell you "how dare you?!"). Idle threads in a (blocking) thread pool do not consume any CPU time and are ready to execute your tasks with the shortest possible delay and overhead because all the necessary resources for the thread were allocated when the thread was spawned.
I would recommend using Intel TBB task scheduler, see a tutorial. It allows for an efficient modern programming paradigm where you partition your problem into stages/tasks, where some of them can be executed in parallel. I cannot recommend enough watching Plain Threads are the GOTO of todays computing - Hartmut Kaiser - Keynote Meeting C++ 2014.

- 131,725
- 17
- 180
- 271