Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do threads by default run on more than one core?

In multi-core processors, and windows application runs many threads. do the threads by default run on more than one core ? I mean every thread might run on individual core.

Edit:

If my application runs many threads, all these threads will run on one process.

Without parallel programming..

Is the process able to devide and run on many core ?

Does my application get benifit of multi-core processing ?


like image 232
Homam Avatar asked Feb 09 '11 08:02

Homam


People also ask

Do threads run on multiple cores?

Instead of giving a large workload to a single core, threaded programs split the work into multiple software threads. These threads are processed in parallel by different CPU cores to save time.

Do threads always run on the same core?

A thread's affinity dictates which CPU cores the OS can schedule the thread to execute on. By default, a thread can be scheduled to execute on any CPU core. When a thread is switched out, it could be scheduled to run on a different CPU core when it's switched back in (that is, when it floats to another core).

Are threads run on the same CPU?

Because a single kernel thread can operate only on a single CPU, the many-to-one model does not allow individual processes to be split across multiple CPUs.

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).


1 Answers

(Your question is fairly unclear. I hope I've answered what you were asking, but if you can clarify the question that would help.)

It's up to the OS where a thread is scheduled. There are advantages to keeping a thread on the same core if possible, in terms of cache coherency etc — but forcing it to stay on the same core is usually overly restrictive.

In short: yes, a thread can run on different cores. Not at the same time, of course - it's only one thread of execution — but it could execute on core C0 at time T0, and then on core C1 at time T1.

EDIT: You talk about an application running many threads, but "without parallel programming" — that's a contradiction in terms. If you're using many threads, you are using parallel programming, and by default those threads can run on multiple cores. If you're only using one thread, then you won't get any benefit from having multiple cores — other than the fact that other processes can use the other cores, of course.

like image 54
Jon Skeet Avatar answered Oct 17 '22 13:10

Jon Skeet