Angel \”Java\” Lopez on Blog

January 25, 2010

AjSharp: Implementing Futures

Filed under: .NET, AjSharp, Open Source Projects, Programming Languages — ajlopez @ 10:55 am

I implemented channels in my AjSharp interpreter. I wrote about it in:

Channels and GoRoutines in AjSharp (part 1)
Channels and GoRoutines in AjSharp (part 2)

I could use channels to implement futures. One option is to add Task Parallel Library from Microsoft, but I should use .NET 4: apparently, the original library is no more available. I guess I could leverage the new classes in the upcoming framework, but meanwhile, I want to explore the ideas of futures programming.

Channels can be used to implement some future process:

channel = new Channel();
go channel <- SomeLengthlyCalculation();
// .. more process
result = <- channel;

But channels are designed to resolve other problems: to have many values during its life, and be used as sync objects between producers and consumers of such values. The above channel variable, if used as a future, could be consulted only one. This is a minor issue, but signals a difference between channels and futures.

So, I wrote a new native classes, accessible by AjSharp programs, Future:

    public class Future : IReference
        private object value;
        private ManualResetEvent handle = new ManualResetEvent(false);
        private bool set = false;
        public void SetValue(object value)
            lock (this)
                if (this.set)
                    throw new InvalidOperationException("Future value already calculated");
                set = true;
                this.value = value;
        public object GetValue()
            return this.value;

In the current version at trunk, IReference interface (SetValue, GetValue) is implemented by Channel class. AjSharp operators a <- b, a = <-b, are mapped to those methods. So, I can use Futures in this way:

future = new Future();
go future <- SomeLenghtlyProcess();
// ... more process
result = <-future;
result2 = <-future;

Consulting the future value is explicit. So, you can pass the variable future to any other function, by name, without viewing its value. I could drop the go in the set of the future, assuming the right value were computed in parallel. But I prefer to make parallelism explicit, using the go command.

This is a minor improvement in parallel features in AjSharp. I was working on queue channels: channels that support the storage of more than one value. You can check the code at:

I want to implement agents, to support actor model programming. It could be nice to have agent as keyword, as class keyword. And that the agents could be run in remote machines.

Keep tuned!

Angel “Java” Lopez

1 Comment »

  1. [...] AjSharp: Implementing Futures [...]

    Pingback by Memory transactions in AjSharp using References « Angel “Java” Lopez on Blog — June 7, 2010 @ 9:08 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme Blog at


Get every new post delivered to your Inbox.

Join 56 other followers

%d bloggers like this: