views:

365

answers:

5

Some of the fellows in the office think that when they've added threads to their code that windows will assign these threads to run on different processors of a multi-core or multi-processor machine. Then when this doesn't happen everything gets blamed on the existence of these threads colliding with one another on said multi-core or multi-processor machine.

Could someone debunk or confirm this notion?

+2  A: 

You generally can only optimally have 1 thread per CPU, but unless your application has some explicit thread affinity to one processor then yes Windows will assign these threads to a free processor.

Xian
your comment is only true when interpreting it "simultaneously", which is a perspective which is far removed from what multi-threading is about.
Charles Bretana
@Charles - Could you expand on this, I can't figure out what you are trying to say here. The statement that one running thread per CPU is optimal is pretty reasonable. And Windows will certainly assign threads to free processors absent any processor affinity restrictions.
Stephen Martin
One running thread per cpu is an oxymoron,. Only one thread can simultaneously run on any cpu, ever. Multi-Threading means more than one thread in a PROCESS, not on a CPU.
Charles Bretana
My apologies, I should have said one 'runnable' thread, I was trying to keep it simple. Unfortunately, your statement still isn't very clear.
Stephen Martin
my apologies, but now you are not clear, (at least to me anyway). What do you mean by a "runnable" thread? but anyway, NO two threads, runnable or not, can "run" on the same CPU at the same time... (how a non-runnable thread could run on one is another question) ...
Charles Bretana
I don't mean to sound harsh but if you don't know what a runnable thread is you're not really qualified to discuss multi-threading(MT). Your answers above and here show an incomplete understanding of MT, something that in MT development is more dangerous than ignorance.
Stephen Martin
@Stephen, I know what runnable, (and MT) means,.. I asked what you meant by it cause yr statement that "The statement that one [runnable] thread per CPU is optimal is pretty reasonable" is silly given what it means. It's like saying "Only 1 LIVING person can breathe from an oxygen tank at once."
Charles Bretana
I mean to be clear, that saying that "... one [runnable] thread per CPU is optimal ... " implies that more than one is NOT optimal ... and that concept is fundamentally - not even wrong. because only ONE thread, whether it's runnnable, not runnable, or comatose, can run on a CPU at the same time.
Charles Bretana
If you have x units of work and y processors where x > y then the optimal number of threads created to process the work is y. If you create fewer threads then you queue up work for sequential processing unnecessarily. If you create more threads then you have losses to excess context switching.
Stephen Martin
Perhaps we are speaking at cross-purposes here, but this is such a basic principal of multi-threaded development that I'm not sure why you can't seem to understand it.
Stephen Martin
+5  A: 

When an application spawns multiple threads, it is indeed possible for them to get assigned to different processors. In fact, it is not uncommon for incorrect multi-threaded code to run ok on a single-processor machine but then display problems on a multi-processor machine. (This happens if the code is safe in the face of time slicing but broken in the face of true concurrency.)

Eric
+2  A: 

Yes, Threads and multi-Threading has almost nothing to do with the number of cpus or cores in a machine...

EDIT ADDITION: To talk about "how many threads run on a cpu" is an oxymoron. Only one thread can ever run on a single CPU. Multi-Threading is about multiple threads in a PROCESS, not on a CPU. Before another thread can be run on any CPU, the thread currently on that CPU has to STOP running, and it's state must be preserved somewhere so that the OS can restart it when it get's it's next "turn".

Code runs in "Processes" which are logical abstractions that can run one or more sequences of code instructions, and manage computer resources independantly from other processes. Within a process, each separate sequence of code instructions is a "thread". Which cpu they run on is irrelevant. A single thread can run on a differnt cpu each time is is allocated a cpu to run on... and multiple threads, as they are each allocated cpu cycles, may, by coincidence, run on the same cpu (although obviously not simultaneously)

The OS (a component of the OS) is responsible for "running" threads. It keeps an in-memory list of all threads, and constantly "switches" (it's called a context switch) among them. It does this in a single CPU machine in almost exactly the same way as it does ion in a multiple-cpu machine. Even in a multiple Cpu machine, each time it "turns on" a thread, it might give it to a different CPU, or to the same cpu, as it did the last time.

Charles Bretana
+2  A: 

Windows will automatically execute multiple threads on different processors if the machine has multiple processors. If you are running on a single processor machine, the threads are time-sliced but when you move the process to a multiple processor machine, the process will automatically take advantage of the multiple processors.

Because the code is running simultaneously, the threads may be more likely to step on each others toes on a multi-core machine then on a single core machine since both threads could be writing to a shared location at the same time instead of this happening if the thread swap is timed just right.

Marcus Erickson
A: 

There is no guarantee that threads of your process will be assigned to run on different CPUs.