用户级线程编程-多个内核线程? [英] User level thread programming - multiple kernel threads?

查看:130
本文介绍了用户级线程编程-多个内核线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多编程语言,例如C ++(posix库)和Java,都提供了与用户级线程一起使用的能力.但是,如果所有这些用户级线程都在单个内核线程中运行-您只会对多程序或多处理(如果有多个处理器可用)有错觉吗?我的意思是-所有这些线程仍在同一内核线程中运行.我说的对吗?而且,如果可以,那么我们到底打算如何使用用户级线程来提高性能?

我猜想在多对一个模型(用户级线程到内核线程映射)上实际上不可能实现性能.因此,在多对多模型中,只有在内核级线程分叉的情况下,才可能提高性能.因此,我的问题是,即使用户级线程的开销很低,我也无法真正实现比调度内核级线程更好的性能提升.

从本质上讲,这就是我要尝试验证的内容-假设计算机有4个处理器.现在,说我的程序是唯一正在运行的程序-并分叉了4个线程,每个线程执行完全独立的操作.现在,如果映射是一对一的(用户到内核的映射),我实际上可以获得4倍的加速,但是如果说(由于某种原因)所有4个用户线程都映射到相同的内核线程空间-则没有加速因为有多个处理,这是因为,即使我有4个用户级线程,它们也运行在相同的内核空间中,并且无法拆分为4个内核.

解决方案

不,您根本不对.

在大多数情况下,将posix用于C/C ++或将Thread实现用于Java以创建和运行线程,这意味着底层的用户空间实现在一个进程的内存空间内运行实际线程.这意味着,在4个CPU的计算机上运行4个线程可以使您真正提高4倍的速度-当然,如果一切都正确编写,并且操作系统本身也没有以某种方式被阻止(阻止)平衡CPU-2线程的使用. /p>

我说大多数情况",因为总会存在无法运行真实线程的POSIX lib(例如某些调试或不完整的实现)或Java线程(例如某些不完整的VM或外来安装程序)的实现-仅模拟这个...但是在标准PC环境中,您可以确定不,您根本不对":)

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.

解决方案

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" :)

这篇关于用户级线程编程-多个内核线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆