|
|
|
|
|
by brainscdf
2305 days ago
|
|
My personal best practice is to always create a thread pool on program startup and distribute your tasks among the thread pool. I use the same best practice in all other languages too. Is this best practice sound or can it lead to problems in some corner cases? |
|
* Do your tasks block? How many threads do you need to make sure you can use all your CPUs.
* Do your tasks access different sets of memory? Would keeping similar tasks on the same CPUs reduce cache misses.
* Do your tasks have different priorities? You might need a pool for each priority.
For a UI program that isn’t doing anything really intensive or real-time, having a common thread pool makes a lot of sense, and can reduce resource use (stacks add up once you get to many 10s or 100s of threads...), and improve latency (a work queue with many threads will get more CPU than another with the same amount of work but fewer threads)