什么是每个CPU线程的实际限制?线程、实际、CPU

2023-09-04 22:42:31 作者:学会抛开过去。

我一直在玩弄线程,试图推动一些限制到了极致 - 我自己的娱乐。我知道线程池默认为25个线程,并且可以推到1000(根据MSDN)。什么虽然是每个CPU核心的线程的实际限制?在某些时候,上下文切换将会导致更多的瓶颈比穿线节省。有没有人有这个覆盖任何的最佳做法?我们在谈论100,200,500?它依靠什么线程都在做什么?是什么决定,比框架结构决定多少线程每个CPU核心优化经营其他?

I've been playing around with threading, attempting to push some limits to the extreme - for my own amusement. I know the threadpool defaults to 25 threads and can be pushed up to 1000 (according to MSDN). What though, is the practical limit of threads per CPU core? At some point, context switching is going to cause more of a bottleneck than threading saves. Does anyone have any best practices covering this? Are we talking 100, 200, 500? Does it depend on what the threads are doing? What determines, other than framework dictated architecture how many threads operate optimally per CPU core?

推荐答案

这一切都取决于什么线程正在做的,当然。如果它们是CPU限制的(比如在一个无限循环坐着不动),那么每个核心一个线程就足够饱和的CPU;比这更多的(并且你已经将有更多的,从后台进程等),你将开始得到的争夺。

It's all dependent on what the threads are doing, of course. If they are CPU-bound (say sitting tight in an infinite loop) then one thread per core will be enough to saturate the CPU; any more than that (and you will already have more, from background processes etc) and you will start getting contention.

在另一个极端,如果线程没有资格来运行(如阻止某些同步对象),那么有多少,你可以有限制将被用于栈,操作系统等因素的影响比CPU(内存决定内部限制,等等)。

On the other extreme, if the threads are not eligible to run (e.g. blocked on some synchronization object), then the limit of how many you could have would be dictated by factors other than the CPU (memory for the stacks, OS internal limits, etc).