27

Green threads were introduced in Erlang and probably all languages based on it know them, also in go (gorutines). Then afaik they were removed from rust.

My questions:

  • How would one implement green threads in .NET? Are there some caveats that prevent current .NET runtime from implementing them?
  • 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)...
riQQ
  • 9,878
  • 7
  • 49
  • 66
stej
  • 28,745
  • 11
  • 71
  • 104
  • 8
    Erlang and Go don't provide green threads. The term has been mis-used too much to be useful. Fibers have not done well in the age of multi-core, an attempt to add them to NET 2.0 was a failure. Use AppDomain to get the kind of state isolation that Erlang and Go provide. Get light-weight threading from the Task class and the async/await keywords. Look at Akka.NET – Hans Passant May 17 '16 at 22:07
  • If you use object pooling to manage tasks and improve performance by caching, you don't need green threads. Poor performance results usually by doing everything async. Mix of async and sync with caching will give you better performance. If green threads were of any great use, I am sure .NET developers would have considered it long ago. – Akash Kava Feb 26 '17 at 14:34
  • @AkashKava You can get quite good performance from "doing everything async", though the language support must be pretty well designed: http://joeduffyblog.com/2015/11/19/asynchronous-everything/ – Kyle Strand Jul 09 '18 at 23:26

3 Answers3

3

How would one implement green threads in .NET? Are there some caveats that prevent current .NET runtime from implementing them?

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); it has recently been achieved in Java, and key folks in .NET have been investigating it.

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)...

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. But as with all performance questions, the answer is in the numbers, and you can't get numbers without an experiment.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

This is super old but worth pointing out: F# has lightweight user-mode threads built in via MailboxProcessor.

Nick Cox
  • 6,164
  • 2
  • 24
  • 30
-2

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. 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).

SACn
  • 1,862
  • 1
  • 14
  • 29