Erlang processes on the CLR
I mentioned that getting to Erlang like processes is something that I really want to get to. I started thinking about how I can make it work with the IEnumerable<T> implementation.
As it turned out, that was fairly simple, once I got the idea. Take a look at this class diagram:
We have tasks, and we register them in the scheduler. We can also wait for them without blocking the thread. Check out this trivial example. We have the following long running task:
public class MultiplicationTask : AbstractTask { private readonly int value; public MultiplicationTask(int value) { this.value = value; } protected override IEnumerable<Condition> Execute() { Thread.Sleep(1000); SetResult(value * 2); yield break; } }
And the code that want to call it:
public class UseMultiplicationTask : AbstractTask { protected override IEnumerable<Condition> Execute() { Future future1 = Spawn(new MultiplicationTask(15)); Future future2 = Spawn(new MultiplicationTask(23)); yield return delegate { return future1.HasValue && future2.HasValue; }; System.Console.WriteLine(future1.GetValue<int>() + future2.GetValue<int>()); } }
We yield a condition that tell us when we can be executed again. From then, it is just a matter of properly scheduling the different tasks.
We can probably pretty it up a bit with lambdas, but this works.
No, instead of listing all the code here, you can browse it here:
https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk/SampleApplications/BlogCode/BlogCode/ErlangTasksI really like this approach
Comments
Having mentioned Parallel Extensions before, have you looked at the CCR? It's got some neat stuff around this kind of thing, including continuation-passing style coding.
http://channel9.msdn.com/Showpost.aspx?postid=143582
There's also the Joins API which could be relevant:
http://research.microsoft.com/~crusso/joins/index.htm?0sr=a
I don't think I'd suggest either of them for production code at the moment (for different reasons) but they're definitely an interesting source of ideas.
Jon
Oren,
I am having trouble with this method. It seems to be missing from the source code.
StealWorkFromAnotherThread() - Referenced in Scheduler.cs
Do a revert to a previous version, that should not have been in the repository, it is an attempt by me to do something that I didn't complete.
Comment preview