tags:

views:

271

answers:

3

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?

+5  A: 

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

LBushkin
I haven't but a quick skim looks pretty interesting.
dkackman
Interesting article, if quite outdated (using VS 2003 style Managed C++).
Reed Copsey
And please note the big red warning on top: Do Not Use This.
Henk Holterman
I also like the mention of undocumented methods to interact with the Cor Runtime
dkackman
In case that link ever goes dead, some context about the article: Title "Implementing Coroutines for .NET by Wrapping the Unmanaged Fiber API" in the September 2003 issue of MSDN Magazine
John K
+6  A: 

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
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
You can implement this using generators in C# natively: http://en.wikipedia.org/wiki/Coroutine#Coroutines_and_generators
Reed Copsey
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
+3  A: 

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

Henk Holterman
Better link than what I posted for using iterators...
Reed Copsey
That is good stuff.
dkackman
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
It means that if you want Fibers as lightweight threads, that is already being done by the Fx and Fx4 will do even more.
Henk Holterman
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
that link is dead.
Henk Holterman
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