Threads and CPU Affinity

1.2k views Asked by At

Lets say there are two processors on a machine. Thread A is running on P1 and Thread B is running on P2.

Thread A calls Sleep(10000);

Is it possible that when Thread A starts executing again, it runs on P2?

If yes, who decides this transition? If no, why not?

Does Processor store some data that which all threads it's running or OS binds each thread to Processor for its full lifetime ?

4

There are 4 answers

0
aqua On BEST ANSWER

It is possible. This would be determined by the operating system process scheduler and may also be dependent on the application that is running. No information about previously running threads is kept by the processor, aside from whatever is in the cache.

0
Kyle Burton On

This is dependent on many things, it behaves differently depending on the particular operating system. See also: Processor Affinity and Scheduling Algorithms. Under Windows you can pin a particular process to a processor core via the task manager.

0
x0n On

Yes, it is possible. Though ultimately a thread inherits its CPU (or CPU core) from the process (executable.) In operating systems, which CPU or CPU core a process runs on for its current quanta (time slice) is decided by the Scheduler:

http://en.wikipedia.org/wiki/Scheduling_(computing)

-Oisin

0
BMitch On

The OS decides which processor to run the thread on, and it may easily change during the lifetime of that thread, especially if there is a context switch (caused by the sleep). It's completely possible if the system is loaded that both threads will be running on the same processor (or core), just at different times. Or if there isn't any load on the system, both threads may continue to run on separate processors.