Monday, September 01, 2008

Using Delegates to Implement APM

In my previous post I described how delegates have evolved over the various versions of the .NET framework. In this post I want to elaborate and describe how to use delegates to implement asynchronous programming.

Many of today's PCs have multiple processors/cores in them, and there's a definite trend among processor makers to gradually increase that number in future models. In order to utilize the increasing number of cores, computer programs need to parallelize their execution, and assign processor intensive tasks to dedicated threads. Writing robust multi-threaded software is not easy. Threads need to be synchronized, data locked, and dead-locks are difficult to avoid. This increases the challenge for developers who need to write robust and efficient multi-threaded code.

To help programmers out, Microsoft introduced the Asynchronous Programming Model which simplifies multi-threaded programming. They have implemented it themselves in many classes throughout the .NET framework such as the FileStream, Socket, WebRequest, SqlCommand, etc. All of these classes offer asynchronous method calls along-side the standard synchronous versions. The async method name always starts with BeginXxx, and offers a corresponding EndXxx. For example, the SqlCommand class offers an ExecuteReader method, plus an async version of the method - BeginExecuteReader. To comply with the APM it also offers an EndExecuteReader method.

All delegates implement this pattern out-of-the-box. The all offer the standard Invoke method to execute the target method, and also offer the BeginInvoke and EndInvoke methods to execute the target method asynchronously.

This is the main reasons I love delegates so much.

Let's get down to it. One of the complexities of multi-threaded programming is figuring out when an async process completed its work. This is where the APM offers a great deal of help. There are three techniques to find out when a BeginInvoke is done: wait, poll and callback. I'll use a sample to describe the three techniques.

class PrimeCalc
{
public int GetNextPrime(int num)
{
int p = num + 1;
while(true)
{
if(IsPrime(p))
{
break;
}
p++;
}
return p;
}
}


The PrimeCalc class has a GetNextPrime method that calculates the next prime number after the number passed in to the method. The class uses some internal methods to do the calculation (I'll add the entire source code at the end of the post) and returns the next prime number it finds. We'll use this method to simulate a compute-intensive task.

Since our target method takes an int as a parameter and returns an int as a return value, we could use the Func< as our delegate:

PrimeCalc calc = new PrimeCalc();
Func<int, int> del = calc.GetNextPrime;


Wait Until Completed Technique



public void WaitUntilComplete(int num)
{
PrimeCalc calc = new PrimeCalc();
Func<int, int> del = calc.GetNextPrime;
IAsyncResult result = del.BeginInvoke(num, null, null);

// Do some other work here

// Suspend this thread until the async operation completes
int prime = del.EndInvoke(result);
}


This is not a very efficient way of using the APM. If the compute-intensive task takes longer than the "other" work that is being done in the mean time, the thread will be suspended and wait for the delegate to return.

Polling Technique



public void Poll(int num)
{
PrimeCalc calc = new PrimeCalc();
Func<int, int> del = calc.GetNextPrime;
IAsyncResult result = del.BeginInvoke(num, null, null);

while (!result.IsCompleted)
{
// Do some other work here
}

// Get the result from the async operation
int prime = del.EndInvoke(result);
}


This option is also not very efficient because the while loop that runs until the async operation completes would consumes CPU resources even after the "other" work that needs to be done is complete.

Callback Technique



This is by far my favorite rendezvous technique of all, it is a bit more complex to implement, but offers greater flexibility and control, and is more efficient in terms of resource management.

public void Callback(int num)
{
DateTime start = DateTime.Now;
Console.WriteLine("Starting async calculation at: {0}", start);

PrimeCalc calc = new PrimeCalc();
Func<int, int> del = calc.GetNextPrime;
del.BeginInvoke(num, CalcCompleted, start);
}

private void CalcCompleted(IAsyncResult result)
{
DateTime start = (DateTime)result.AsyncState;

Func<int, int> del = (Func<int, int>)((AsyncResult)result).AsyncDelegate;
int prime = del.EndInvoke(result);

DateTime end = DateTime.Now;
TimeSpan length = end.Subtract(start);
Console.WriteLine("Completed async calculation at: {0}", end);
Console.WriteLine("Async calculation took {0:F} seconds.", length.TotalSeconds);
}


The basic idea here is that the delegate calls the callback method when it completes. The signature of the callback method should match the AsyncCallback delegate signature:

private void CalcCompleted(IAsyncResult result)


This time, when we call the BeginInvoke method on our delegate, we pass in the callback method as the second parameter, and the third parameter could be any state object we would like to pass in to the EndInvoke method. In this sample I passed in the start DateTime of the async opertaion.

In the callback method you should get a reference to the delegate instance (by casting the result param to the appropriate type), and use it to call EndInvoke. It is important to call EndInvoke even if your method does not return a value. This prevents reasource leaks.

You get a reference to the state object you passed in the BeginInvoke 3rd parameter by casting the result's AsyncState property to the appropriate type.

Here is the entire PrimeCalc class source code:


namespace AsyncProgrammingModel
{
class PrimeCalc
{
private List<int> primes;

public PrimeCalc()
{
primes = new ListList<int>();
primes.Add(2);
primes.Add(3);
}

public int GetNextPrime(int num)
{

int p = num + 1;
while (true)
{
// Create a list of all prime numbers up to p.
AddPrimesToList(p);
if (IsPrime(p))
{
break;
}
p++;
}
return p;
}

private void AddPrimesToList(int numberToTest)
{
int n = primes[primes.Count - 1] + 2;
while (n < numberToTest)
{
if (IsPrime(n))
{
primes.Add(n);
}

// Skip even numbers.
n += 2;
}
}

private bool IsPrime(int n)
{
bool foundDivisor = false;
bool exceedsSquareRoot = false;

int i = 0;
int divisor = 0;

// Stop the search if:
// there are no more primes in the list,
// there is a divisor of n in the list, or
// there is a prime that is larger than the square root of n.
while ((i < primes.Count) && !foundDivisor && !exceedsSquareRoot)
{
// The divisor variable will be the smallest
// prime number not yet tried.
divisor = primes[i++];

// Determine whether the divisor is greater than the square root of n.
if (divisor * divisor > n)
{
exceedsSquareRoot = true;
}
// Determine whether the divisor is a factor of n.
else if (n % divisor == 0)
{
foundDivisor = true;
}
}

return !foundDivisor;
}
}
}

0 comments: