Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a thread run on the same CPU/core throughout its life?

In Windows on a multiprocessor machine, does a thread change the CPU/core during its lifetime?

like image 803
bytefire Avatar asked Aug 28 '13 19:08

bytefire


People also ask

Do threads run on the same core?

Yes, threads and processes can run concurrently on multi-core CPUs, so this works as you describe (regardless of how you create those threads and processes, OpenMP or otherwise). A single process or thread only runs on a single core at a time.

Can different threads run on different processors?

Can two threads be assigned to different processors and execute at the same time? Yes. A couple of threads may be sharing a processor core, with one running while the other waits. Modern CPUs often have hyper-threading technology to make this switching back and forth quite cheap (little overhead, fast performance).

Is a CPU thread the same as a core?

KEY DIFFERENCECores is an actual hardware component whereas thread is a virtual component that manages the tasks. Cores use content switching while threads use multiple CPUs for operating numerous processes. Cores require only a signal process unit whereas threads require multiple processing units.

How many threads can a CPU core run?

A single CPU core can have up-to 2 threads per core. For example, if a CPU is dual core (i.e., 2 cores) it will have 4 threads.


2 Answers

Yes, by default a thread can be scheduled on any available core. You can set the thread affinity if you want to restrict a thread to a specific core or cores.

like image 155
Ted Mielczarek Avatar answered Nov 15 '22 05:11

Ted Mielczarek


does a thread change the CPU/core during its lifetime?

It can. It doesn't necessarily change, but there is nothing preventing the operating system from moving a thread between cores.

The Windows API does provide some control over this via SetThreadIdealProcessor or SetThreadAffinityMask and SetProcessAffinityMask, if you need to control a thread's operation.

like image 25
Reed Copsey Avatar answered Nov 15 '22 05:11

Reed Copsey