Number of processor core vs the size of a thread pool

前端 未结 4 1658
耶瑟儿~
耶瑟儿~ 2021-02-05 10:11

Many times I\'ve heard that it is better to maintain the number of threads in a thread pool below the number of cores in that system. Having twice or more threads than the numbe

4条回答
  •  执念已碎
    2021-02-05 10:23

    That's not true, unless the number of threads is vastly more than the number of cores. The reasoning is that additional threads will mean additional context switches. But it's not true because an operating system will only make unforced context switches if those context switches are beneficial, and additional threads don't force additional context switches.

    If you create an absurd number of threads, that wastes resources. But none of this is anything compared to how bad creating too few threads is. If you create too few threads, an unexpected block (such as a page fault) can result in CPUs sitting idle, and that swamps any possible harm from a few extra context switches.

提交回复
热议问题