开发者

Relation of the words :: 'Thread' and 'Core'

Does the number of threads created/able by a program开发者_如何学Go has any relation/constrain with the number or core that CPU has? Does the word thread has any relation with Cores of CPU at all?


A computer can only run number_of_cores threads in parallel. If a system has only a single core, only one thread runs at once. Multitasking is only "simulated" by running each thread consecutively for very short periods of time.

Normally the OS schedules threads arbitrarily on cores. For instance a thread's one quantum could be run on one core and the same thread's next quantum could be run on another core. That's why you see both cores busy when you run a single cpu-heavy thread. This allows single-threaded processes take advantage of multicore systems by distributing load among multiple cores, depending on varying availability.

More than that, operating systems allow a thread to be "locked" on one or more cores, so it runs dedicated on specified cores only. That's called affinity mask on Windows. Today's multicore aware software can take advantage of that and dedicate certain threads to certain cores to optimize its workload. Games do that for dedicating AI for one core, rendering for another for instance. In order to do that efficiently the software has to know how many cores a system has and create and assign threads based on that number.

Similarly parallel programming frameworks such as OpenMP count the cores to parallelize operations efficiently. They create as many threads as the number of cores on the system to get optimal performance for physical parallelism.

I think that's the only case of a relation between number of cores and number of threads.


The number of cores on a machine is the number of independent processing units that are capable of running at the same time.

The number of threads that a program can spawn is the number of logical units of execution that the program would like to run in parallel with each other.

Typically, the number of threads a program can spawn is limited by the language runtime and the operating system's internal threading implementation. However, if you spawn more threads than there are cores on the machine, not all of those threads can be running in parallel with one another. Instead, the threads will be scheduled in some fashion to try to maximize the amount of time that each thread is running. It's fine to have more threads than cores, but you won't necessarily get much of a speedup by introducing new threads once all the cores are running threads at the same time.

In many cases, a program will have many hundreds of threads, only a small number of which will be running at any time. The other threads might be sleeping and waiting for some event to occur (user input, a timer, a network or disk event, etc.). This isn't a problem; since those threads aren't actively doing anything, the machine doesn't need to dedicate any cores to them, and can spend its processing power working on other threads.


Each core can execute one thread. If you have X cores, then X threads may execute simultaneously. If you have X cores and X + n threads, then at most X threads will execute simultaneously. However, all threads will execute, as the operating system kernel will share the cores among all of the threads.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜