Angel \”Java\” Lopez on Blog

January 27, 2010

Queue Channels in AjSharp

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

Some weeks ago, I implemented channels in my AjSharp interpreter. You can read about that implementation:

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

If you read the value of a channel, and there is no value, the read thread is blocked until a value is available. This is a way to synchronize the consumers and producers that operates over the channel. But sometimes, you want to put a value on a channel and continue. So, I added a “queue channel” (I could named it queued channel): you can put N values in the channel without blocking, saving them in an internal queue. The size of the queue is set in the constructor of the channel. The queue is limited in size, in order to preserve the synchronization features.

The name of the new class is QueueChannel. A test:

        [TestMethod]
        public void CreateAndUseQueueChannelWithTenElements()
        {
            QueueChannel channel = new QueueChannel(10);
            for (int k = 1; k <= 10; k++)
                channel.Send(k);
            for (int k = 1; k <= 10; k++)
                Assert.AreEqual(k, channel.Receive());
        }

The channel receives 10 values without blocking. If you send more values, you should use another thread:

        [TestMethod]
        public void CreateAndUseQueueChannelWithMoreEntriesThanSize()
        {
            QueueChannel channel = new QueueChannel(10);
            Thread thread = new Thread(new ThreadStart(delegate()
            {
                for (int k = 1; k <= 20; k++)
                    channel.Send(k);
            }));
            thread.Start();
            for (int k = 1; k <= 20; k++)
                Assert.AreEqual(k, channel.Receive());
        }

I rewrote my prime example using queue channels:

numbers = new QueueChannel(10);
running = true;
k = 1;
go while(running) { k++; numbers <- k; }
function filter(in, out, prime)
{
  while (true) 
  {
    value = <-in;
    if (value % prime)
      out <- value;
  }
}
function makefilter(channel, number)
{
  newchannel = new QueueChannel(10);
  go filter(channel, newchannel, number);
  return newchannel;
}
channel = numbers;
number = <-channel;
while (number < 1000) 
{
  PrintLine("Prime " + number);
  
  channel = makefilter(channel, number);
  
  number = <-channel;
}
running = false;

The code is in my Google code project:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjLanguage

My motivation for a queue channel, is that I'm writing a parallel genetic algorithm example, that suffered of too many blockings. I feel that a good algorithm doesn't need queues, but if you have many consumers and producers, and one thread produce many values to various channels, using common channel could raise blockings. Another way, to avoid blocking, is using the go command:

go channel <- newvalue;

But I guess it's over killing to launch a goroutine only to feed a channel.

I should present the parallel genetic algorithm, write about ideas of implementing actor model in AjSharp, and about distributed agents.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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 )

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. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 66 other followers

%d bloggers like this: