简体   繁体   English

.NET 中的绿色线程

[英]Green threads in .NET

Green threads were introduced in Erlang and probably all languages based on it know them, also in go (gorutines).绿色线程在 Erlang 中引入,可能所有基于它的语言都知道它们,也在 go(gorutines)中。 Then afaik they were removed from rust.然后 afaik 他们从 rust 中删除。

My questions:我的问题:

  • How would one implement green threads in .NET?如何在 .NET 中实现绿色线程? Are there some caveats that prevent current .NET runtime from implementing them?是否有一些警告会阻止当前 .NET 运行时实现它们?
  • Does it even makes sense in terms of performance?它甚至在性能方面有意义吗? We have a quite lightweight Task and in (near) future we will have even ValueType Task (more suitable for some scenarios)...我们有一个非常轻量级的任务,在(不久的)将来我们甚至会有 ValueType 任务(更适合某些场景)......

In computer programming, green threads are threads that are scheduled by a runtime library or virtual machine (VM) instead of natively by the underlying operating system.在计算机编程中,绿色线程是由运行时库或虚拟机 (VM) 而非由底层操作系统本地调度的线程。 Managed Threads written with NET Framework will be scheduled by framework but whatever be the case Windows OS will be running beneath and attaching threads to CPU ( as NET requires Windows ).使用NET Framework编写的托管线程将由框架调度,但无论如何 Windows 操作系统将在下面运行并将线程附加到 CPU(因为 NET 需要 Windows )。

How would one implement green threads in .NET?如何在 .NET 中实现绿色线程? Are there some caveats that prevent current .NET runtime from implementing them?是否有一些警告会阻止当前 .NET 运行时实现它们?

In the .NET runtime, by layering a virtual thread on top of OS threads, and dealing with all the side-effects / niche cases (persisting stack-frames, dealing with affinity, and a lot more);.NET运行时,通过在 OS 线程之上分层虚拟线程,并处理所有副作用/小众情况(持久化堆栈框架、处理亲和力等); it has recently been achieved in Java, and key folks in .NET have been investigating it .它最近在 Java 中实现了,并且.NET 中的关键人物一直在调查它

Does it even makes sense in terms of performance?它甚至在性能方面有意义吗? We have a quite lightweight Task and in (near) future we will have even ValueType Task (more suitable for some scenarios)...我们有一个非常轻量级的任务,在(不久的)将来我们甚至会有 ValueType 任务(更适合某些场景)......

This is a tricky one to answer without experimenting;如果不进行实验,这是一个很难回答的问题; green threads have the advantage that it would reach a lot more code, at once;绿色线程的优点是它可以同时访问更多的代码; but that's also a disadvantage in terms of risk / complexity.但这在风险/复杂性方面也是一个劣势。 The machinery involved in async/await is not trivial , so if that can be elided: great, But in some ways, it is simply moved , albeit it to specialized code that could be heavily optimized. async/await中涉及的机制不是微不足道的,所以如果可以省略:很好,但在某些方面,它只是被移动了,尽管它是可以高度优化的专用代码。 But as with all performance questions, the answer is in the numbers, and you can't get numbers without an experiment.但与所有性能问题一样,答案就在数字中,没有实验就无法获得数字。

这是非常古老但值得指出的:F# 具有通过MailboxProcessor内置的轻量级用户模式线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM