线程是在C#用户级别还是在内核级别创建的? [英] Is the thread created in C# user level or kernel level?

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

问题描述

最近我正在研究操作系统,并遇到了这个问题.

Recently I was studying the operating system, and came across this question.

是在C#用户级别或内核级别创建的线程,例如:

Is the thread created in C# user level or kernel level, like:

Thread mythread=new Thread(ThreadStart(something));

据我所知,内核级别上具有CPU密集型线程的进程的运行速度可能比用户级别上的更快.级线程不会陷入内核,这就是为什么它们比内核级线程更轻量的原因."
因此,我认为用户级线程无法在不同的cpu上运行,这需要捕获到内核中.

As far as I know, the process with cpu-intensive thread on kernel level could run faster than those on user level. Because the book Modern operating system says "The schedule of user-level thread will not trap into kernel,that's why they are more lightweight compared with kernl-level thread".
So I think user-level thread could not run on different cpu,which requires trapping into kernel.

在Linux中,由pthread_create创建的线程是内核级别的.因此,我对.Net C#的功能感到好奇.

And in linux,the thread created by pthread_create is kernel level. So I'm curious about the feature of .Net C#.

推荐答案

这是Unix术语,在Windows上您会说光纤或线程".术语绿色线程"也是说用户线程"的一种很常见的方式.

That is unix terminology, on Windows you'd say "fiber or thread". The term "green thread" is also a pretty common way to say "user thread".

这不是由C#还是CLR来决定,这是由 CLR主机决定的.

It is not up to C# nor the CLR to decide this, it is the CLR host that determines this.

主机是将CLR连接到操作系统或主机进程的粘合剂.针对Silverlight,.NET Compact,.NETCore,Xbox,Windows Phone,Hololens等的程序始终具有自定义主机,以适应目标操作系统. IIS和SQL Server是非托管程序的常见示例,这些托管程序具有自定义主机以允许托管代码执行,分别是ASP.NET和CLR存储过程.许多其他程序都允许使用自定义主机在C#中编写脚本,AutoCAD是一个典型示例.

The host is the glue that marries the CLR to the operating system or the host process. Programs that target Silverlight, .NET Compact, .NETCore, Xbox, Windows Phone, Hololens, etc always have a custom host to adapt to the target's OS. IIS and SQL Server are common examples of unmanaged programs that have a custom host to allow managed code execution, respectively ASP.NET and CLR stored procedures. Lots of other programs allow scripting in C# with a custom host, AutoCAD is the canonical example.

因此CLR不会自己创建线程,而是要求主机执行该操作. ICLRTask ICLRTaskManager 接口可以完成这项工作.线程池也是主机任务, ICorThreadpool界面.

So the CLR does not create a thread itself, it asks the host to do it. The ICLRTask and ICLRTaskManager interfaces get that job done. The thread pool is a host duty as well, ICorThreadpool interface.

因此,您将获得光纤或线是正式不可知的.值得注意的是,这些接口是应SQL Server团队的要求添加的.他们当时在光纤上投入了大量资金,并希望选择在光纤上执行CLR存储过程.一切都做完了,但几乎同时,在2000年代初的多核革命使那辆苹果购物车感到不安.他们实际上并没有发货.我不知道有任何使用光纤的主机,尽管您无法确定自定义主机是常见的.

So it is formally unknowable that you'll get a fiber or thread. Notable is that these interfaces were added at the request of the SQL Server team. They were heavily invested in fibers at the time and wanted the option to execute CLR stored procedures on a fiber. Got it all done, but at roughly the same time the multi-core revolution of the early 2000s upset that apple-cart. And they did not actually ship it. I am not aware of any host that uses fibers, albeit that you can never be sure with custom hosting being common.

因此,假设您将获得内核线程"是非常安全的.

So it is pretty safe to assume that you'll get a "kernel thread".

这篇关于线程是在C#用户级别还是在内核级别创建的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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