AjSharp: Implementing Futures

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



One thought on “AjSharp: Implementing Futures

  1. Pingback: Memory transactions in AjSharp using References « Angel “Java” Lopez on Blog

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s