User level thread programming - multiple kernel threads?

吃可爱长大的小学妹 提交于 2020-01-25 09:10:52

问题


Many programming languages like C++ (posix library) and Java provide the ability to play around with user-level threads. However, if all these user-level threads run in a single kernel thread - you only get the illusion of multiprogramming or multiprocessing (if multiple processors available) right ? I mean- still all these threads run in the same kernel thread. Am I right in saying that? And, if yes, then how exactly do we plan to get performance improvement using user-level threads ?

EDIT: I guess performance would not really be possible on a many to one model (user level threads to kernel thread mapping). So in a many to many model, performance improvement is possible only if a kernel level thread forks off. So my question is, even if user level threads have low overhead, I cannot really envisage a performance improvement as great as scheduling kernel level threads.

EDIT2: Essentially this is what I am trying to get verified - "Assume a computer has 4 processors. Now, say my program is the only thing running - and has forked 4 threads each of which do completely independent things. Now, if the mapping is say one to one (user to kernel mapping), I can actually get a perfect 4 times speedup. However if say (for some reason) all 4 user threads map to the same kernel thread space - then there is no speedup because of multiprocessing. This is because, even though I have 4 user level threads - they run in the same kernel space and cannot be split across 4 cores.


回答1:


No, you are simply not right.

In most cases use of posix for C/C++ or Thread implementation for Java to create and run threads, means that underlying user-space implementation runs real threads within the memory space of one process. It means, that running 4 threads on 4 CPU's machine gives you a real 4x speed-up - of course if everything is properly written, and the OS itself is not somehow blocked (prevented) from balancing the CPU-2-thread usage.

I said "most cases", because there can be always implementation of POSIX lib (e.g. some debug or incomplete implementation) or Java Threads (e.g. some incomplete VM or exotic setup) which won't be running real threads - just simulating this... But on standard PC environment, you can be sure "No, you are simply not right" :)



来源:https://stackoverflow.com/questions/7125151/user-level-thread-programming-multiple-kernel-threads

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