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?

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Actually, fibers can be used to implement relatively inexpensive coroutines (see: en.wikipedia.org/wiki/Coroutine and 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.
You can implement this using generators in C# natively: en.wikipedia.org/wiki/Coroutine#Coroutines_and_generators
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...
@ReedCopsey the problem is that those are stackless coroutines, whereas fiber-based ones are stackful.
@rightfold What advantages does a "stackful" coroutine have over a "stackless" one?
|
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

5 Comments

I haven't but a quick skim looks pretty interesting.
Interesting article, if quite outdated (using VS 2003 style Managed C++).
And please note the big red warning on top: Do Not Use This.
I also like the mention of undocumented methods to interact with the Cor Runtime
The link is invalid, it just links to a collection of magazine issues.
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.

4 Comments

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. go.microsoft.com/fwlink/?linkid=14202' "aka lightweight threads" is interesting.
It means that if you want Fibers as lightweight threads, that is already being done by the Fx and Fx4 will do even more.
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).
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. blogs.msdn.com/ericlippert/archive/2009/07/09/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.