14

Out of more curiosity than anything I've been looking for a set of C#/.net classes to support fibers/co-routines (the win32 version) and haven't had any luck.

Does anybody know of such a beast?

dkackman
  • 15,179
  • 13
  • 69
  • 123

3 Answers3

12

No. There isn't a Fiber API in the Framework. I suspect this is because there is little advantage to using them - even the fiber API page (native) mentions:

In general, fibers do not provide advantages over a well-designed multithreaded application.

.NET makes it so much easier to develop a "well-designed" multithreaded application that I suspect there is little use for a fiber API.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 5
    Actually, fibers can be used to implement relatively inexpensive coroutines (see: http://en.wikipedia.org/wiki/Coroutine and http://en.wikipedia.org/wiki/Fiber_%28computer_science%29). Until c# natively supports coroutines as a language features, fibers are probably the next easiest way to get there. – LBushkin Dec 22 '09 at 21:00
  • You can implement this using generators in C# natively: http://en.wikipedia.org/wiki/Coroutine#Coroutines_and_generators – Reed Copsey Dec 22 '09 at 21:16
  • Mix generators with some of the new things like Rx and TPL, and I think you'd be hard pressed to find a good use of fibers in C# now... – Reed Copsey Dec 22 '09 at 21:17
  • 4
    @ReedCopsey the problem is that those are stackless coroutines, whereas fiber-based ones are stackful. –  Feb 12 '15 at 09:13
  • 2
    @rightfold What advantages does a "stackful" coroutine have over a "stackless" one? – MathuSum Mut Jan 08 '17 at 17:13
  • Aaaand, in 2018, we now have native support for async/await. – henry700 Jul 06 '18 at 03:43
  • @henry700 That has been present in one form or another for about 10 years. Syntactic sugar came along in 2012-ish time frame. Think 2008, when tasks library was shipped for .net framework 3.5 alongside Rx. – Tanveer Badar Nov 22 '18 at 18:57
  • When i commented async/await meant the syntax sugar, not the Task library. – henry700 Nov 22 '18 at 19:35
  • @MathuSumMut In a "stackless" coroutine (aka, with `async`/`await` or `yield`), every function that might yield must be marked as such and its call prepended with a token (in this case, `await`). This can be a problem, for example, when you're handing it to a tech-designer/gameplay programmer and want to hide the complexity of the "script-functions" away (like, for example, using the async/await connection to exchange messages). – JoaoBapt May 23 '21 at 22:08
9

Have you seen this:

Title "Implementing Coroutines for .NET by Wrapping the Unmanaged Fiber API"
in the September 2003 issue of MSDN Magazine

http://msdn.microsoft.com/en-us/magazine/cc164086.aspx

H H
  • 263,252
  • 30
  • 330
  • 514
LBushkin
  • 129,300
  • 32
  • 216
  • 265
8

If I remember correctly, there was one in the .NET 2 beta, but it was dropped. Eric Lippert wrote about fibers and continuations and said they chose the smallest necessary (link).

There are ways to use iterators and yield to make a coroutine system, see this link. And another one from Joe Duffy.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    Interestingly enough I was playing around with the code form the linked MSDN article (above) and got this warning (.net 4 beta): warning CS0618: 'System.AppDomain.GetCurrentThreadId()' is obsolete: 'AppDomain.GetCurrentThreadId has been deprecated because it does not provide a stable Id when managed threads are running on fibers (aka lightweight threads). To get a stable identifier for a managed thread, use the ManagedThreadId property on Thread. http://go.microsoft.com/fwlink/?linkid=14202' "aka lightweight threads" is interesting. – dkackman Dec 23 '09 at 02:35
  • It means that if you want Fibers as lightweight threads, that is already being done by the Fx and Fx4 will do even more. – H H Dec 23 '09 at 09:24
  • I'm not sure I interpret it that way. Given the linked article from Lippert this looks like residue from the built in fiber support, hence removed. (This message from is Fx4 btw). – dkackman Dec 23 '09 at 13:16
  • I meant this one (linked above) where support for fibers in the framework is discussed. On an additional note, the stuff that the original msdn author had to do with the runtime doesn't appear necessary with the 4.0 runtime which leads me to believe that some of the support they worked on remains intact. http://blogs.msdn.com/ericlippert/archive/2009/07/09/iterator-blocks-part-one.aspx – dkackman Dec 23 '09 at 18:28