8

So I was chatting with a colleague about fibers and turned up this paper from 2003 that describes a implementation of coroutines in C# using the Fiber API.

The implementation of Yield in this paper was for .NET 1.1, so it predates the yield return syntax that appeared in .NET 2.0.

It definitely looks, at first glance, that the implementation here is potentially faster and could scale across multiple CPUs rather well.

Has anyone used it?

Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
  • 2
    I haven't used it, but I have an interest in the subject. Here's one nice implementation of coroutines in c# with a round-robin scheduler: http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=71235c5a-3753-4bab-bdb0-334ab439afaf – jpbochi Dec 10 '09 at 17:36
  • 2
    BTW, what kind of answer do you expect for this question? – jpbochi Dec 10 '09 at 17:37
  • I haven't used it, but the article was interesting. The problem is that this seems to have largely been implemented now, in Windows. – James Black Dec 10 '09 at 17:41
  • 1
    "potentially faster" than what? – H H Dec 10 '09 at 18:02
  • And please make the text and the title of the question match. – H H Dec 10 '09 at 18:06
  • I think Jeremy meant "potentially faster than C# iterators" – jpbochi Dec 10 '09 at 18:22
  • 1
    @jpbochi: yes, indeed, faster than C# iterators. I'm expecting answers of the kind you've given: coroutines are new to me, and so are fibers, and I'm intrigued to see if they're relevant for use in high-performance systems. – Jeremy McGee Dec 10 '09 at 19:46
  • @Jeremy: I'll put my comment as an answer then. :) – jpbochi Dec 14 '09 at 16:17

3 Answers3

8

I haven't used it, but I have an interest in the subject. Here's one nice implementation of coroutines in C# with a round-robin scheduler: http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=71235c5a-3753-4bab-bdb0-334ab439afaf

By the way, quoting wikipedia, "fibers describe essentially the same concept as coroutines". As far as I know, the closest thing to a coroutine (or a fiber) in C# are iterators. Actually, they are remarkably close to coroutines. Lippert posted several catches about iterators. Hopefully, none of them represent an serious problem for the purposes you need.

jpbochi
  • 4,366
  • 3
  • 34
  • 43
7

I have used yield-based "coroutines," and I have to say that they are a pain in the butt. The problem is, of course, that everywhere you want to use them, you're forced to use the yield syntax. Not only that, but unless you chain yields (parent yields child's yield), you can only ever nest your coroutines one level deep. This completely destroys one of the key benefits of coroutines (full stack save/restore).

I implemented a fiber-based coroutine system in C# and it worked wonderfully UNTIL I hit an exception. Unfortunately the .Net runtime stores a bunch of internal exception stuff in OS threads, which means that emulating multiple threads using OS fibers (and p/invoke) just won't work unless you'll never, ever, ever have an exception.

Tom C
  • 79
  • 1
  • 1
  • Could you work around the lack of exception handling in unmanaged code, by adding a try/catch around every piece of managed code, before the yield? – Contango Jan 27 '12 at 17:04
  • 1
    This answer is important. It means that you can never use fiber-based coroutines in production. – usr Jan 29 '12 at 12:44
  • Actually it just means you have to nest your execution, support handled and unhandled exceptions alike as you execute, propagating the exception ONLY completely when unhandled. Exception based programming concepts should also be supported through a Fiber concept or a suitable derivative. – Jay Jul 16 '16 at 15:31
2

Coroutines, at very first glance catches my attention.. some days ago i was searching for workflow solution for parrallel AsyncWCF Method calls and what i found was really fascinating:

http://csharperimage.jeremylikness.com/2010/03/sequential-asynchronous-workflows-in.html

this article shows a very good use of coroutines to create/manage workflows in Silverlight application that consumes WCF using Async Pattern.

I don't know its speed w.r.t to iterators but to me its like an advanced form of subroutines that can be very helpful in mission critical tasks where a normal subroutine can't offer you the luxury to perform a task in parallel.

Shoaib Shaikh
  • 4,565
  • 1
  • 27
  • 35