Linux: Processes and Threads in a Multi-core CPU

放肆的年华 提交于 2021-02-07 07:35:20

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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)




回答6:


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.



来源:https://stackoverflow.com/questions/3395588/linux-processes-and-threads-in-a-multi-core-cpu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!