用户级线程比顺序进程有何优势?
用户级线程有什么用?我读过一些文章说用户级线程比内核级线程更快。用户级线程是非抢占式的,并且阻塞一个用户级线程会阻塞进程中的所有其他用户级线程。
这意味着不能出现一个用户级线程正在做IO而另一个用户级线程正在执行的情况。此外,由于由用户级线程来控制,因此用户级线程也不能用于 GUI。
问题是:用户级线程比顺序进程有什么好处?
What is the use of user level threads? I have read articles saying that user level threads are faster than kernel-level threads. User-level threads are non-preemptive and blocking of one user-level thread blocks all other user-level threads in the process.
This means that there cannot be a situation in which one user-level thread is doing IO and another user-level thread is executing. Also, as it is up to the user level thread to yield control, the user-level threads cannot be used for GUIs as well.
The question is: How are user-level threads any better than a sequential process ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您通常将用户级线程与事件循环一起使用,以便其他用户级线程在等待数据时可以继续执行:当线程让出时,线程调度程序会轮询已注册的文件描述符以了解就绪情况,并且通常会优先考虑该线程( s) 输入已准备好。同时,非自动yield有一个很大的优势:你通常不必担心对数据结构的并发访问(除非程序员很愚蠢并且无缘无故地在相对于其他线程的原子操作中yield) )。这意味着更少(通常不需要)同步和锁定,这就是用户级线程通常胜过内核线程的原因:开销要低得多。当需要同步时,它通常比内核线程更便宜。
You normally use user-level threads with an event loop, such that other user-level threads can continue executing while one is waiting for data: the thread scheduler polls registered file descriptors for readiness when a thread yields, and will usually prioritize the thread(s) for which input is ready. Meanwhile, the non-automatic yield has a big advantage: you often don't have to worry about concurrent access to data structures (unless the programmer is stupid and gratuitously yields in the middle of what should be an atomic operation with respect to other threads). This means less need (often no need) for synchronization and locking, which is why user-level threads often win over kernel threads: much lower overhead. And when synchronization is needed, it's often cheaper than with kernel threads.
一个可能的好处是:设计/代码组织。通过使用线程构造,我们可以非常清楚地了解独立的处理部分以及它们需要交互的位置。
One possible benefit: design/code organisation. By using a Thread construct we make very clear the independent pieces of processing and the places at which they need to interact.