views:

104

answers:

6

Is it true that threads, compared to processes, are less likely to benefit from a multi-core processor? In other words, would the kernel make the decision of executing threads on a single core rather than on multiple cores?

I'm talking about threads belonging to the same process.

+2  A: 

Multiple single-threaded processes are more expensive to the system than single multi-threaded ones. But they will benefit from multicore CPU with same efficiency. Plus inter-thread communication is much cheaper then inter-process communication. If these threads really form single application i vote for multithreading.

Andrey
"But they will benefit from multicore CPU with same efficiency." By "they", are you talking about processes or threads? The "but" confused me, sorry.
someguy
+2  A: 

That's news to me. Linux in particular makes little distinction between threads and processes. They are really just processes that share their address-space.

Marcelo Cantos
Apparently it's written somewhere in the documentation. I'd rather take your word for it than a vague rumour, though.
someguy
Look at the clone() system call.
Marko Kevac
+4  A: 

I don't know how the (various) Linux scheduler handle this, but inter-thread communication gets more expensive when threads are running on different Cores.

So the scheduler may decide to run threads of a process on the same CPU if there are other processes needing CPU time.

Eg with a Dual-Core CPU, if there are two processes with two threads and all are using all CPU time they get, it is better to run the two threads of the first process on the first Core and the two threads of the other process on the second core.

IanH
I found this to be really helpful, thanks.
someguy
A: 

Whereas Windows uses fibres and threads I sometimes think Linux uses processes and twine. I've found that in writing multi-threaded processes you really have to be rigorous, pedantic, disciplined and bloody-minded in designing threaded processes in order for them to achieve a balance of benefit in using whatever number of cores are available on the machine that the process is to run on.

Is it true, on Linux, that threads, compared to processes, are less likely to benefit from a multi-core processor? No one knows.

Sam
A: 

Shared-memory multithreading imposes huge complexity costs on everything from your tool-chain, to development, to debugging, reasoning, and testing your code. NEVER use shared-memory multithreading where you can reasonably use a multi-process design.

@Marcelo is right, any decent OS will treat threads and processes very similarly, some cpu-affinity for threads may reduce the multi-processor usage of a multi-threaded process, but you should see that with any two processes that share a common .text segment as well.

Pick threads vs. processes based on complexity and architectural design constraints, speed will almost never come into it.

Recurse
I agree that design should come first, but you're contradicting yourself when you say "never use shared-memory multithreading". I understand what you mean by threads being more complex, but it's an exaggeration. I'm going to stick with using threads, unless it makes sense to use processes (maybe for a "multi-document" application).
someguy
+1  A: 

It actually all depends on the scheduler, type of multiprocessing, and current running environment.

Assume nothing, test, test, test!

If you're the only multi-threaded process on the system, multi-threading is generally a good idea.

However, from the perspective of the ease of development, sometimes you want separate address spaces and shared data, especially in NUMA systems.

One thing for sure: If it's a 'HyperThreaded' system, threads are much more efficient by virtue of close memory sharing.

If it is a regular multi-core processing.. it should be similar.

If it is a NUMA system, you're better off keeping data shared and code separate. Again, it's all architecture dependent, and it doesn't matter performance-wise unless you're in the HPC business.

If you are in the HPC (supercomputing) business, TEST!. It's all machine dependent (and benefits are 10-25% on average, it matters if you're talking days of difference)

qdot