November 24, 2014
Hot Topics:

Building Responsive .NET Applications with Microsoft Concurrency and Coordination Runtime (CCR)

  • July 23, 2009
  • By Jeffrey Juday
  • Send Email »
  • More Articles »

The Arbiter and Port class comprise the other main CCR classes.

A developer works through an Arbiter class to configure how a delegate function is executed by the Dispatcher. Through an Arbiter, for example, a developer can configure a function to run exclusively thus addressing the shared state aspect of Concurrency. Arbiter methods also bind operations to a Dispatcher Queue.

Port is a sort of entry point in the CCR. Once a developer has created a Dispatcher, Dispatcher Queues, and enlisted an Arbiter to lay out how a delegate should be executed; everything is initiated through a Port. Ports are generic classes. The Port Post method receives a predefined class that, in turn, initiates the whole process.

Now I'm going to put the CCR to work on an APM solution using the Ping class built into the .NET Framework.

One Ping Only

Large amounts of Pinging with the .NET Ping class nicely illustrates the need for APM functions. Pinging an unused IP address will eventually fail, but only after a timeout. Pinging a series of IP addresses synchronously will execute in the total duration it takes to complete all the Pings. Pinging asynchronously will execute in the total duration of the longest Ping.

Pinging asynchronously results in a big performance difference if, for example, you're Pinging a wide range of IP addresses and most of the addresses are unused.

My Console application sample Pings some URLs and returns the results.

Clearly, I didn't need CCR to do APM Pinging. So, I imposed two requirements on my sample that really illustrate when CCR can be a better platform for APM. My special requirements are as follows:

  1. I wanted exceptions to be given a higher priority than successful Pings.
  2. I wanted to avoid doing locking, but I still wanted to update state.

Now let's look at some code!

Dispatcher and DispatcherQueue

CCRMediator class encapsulates most of the sample application. The class's constructor creates the CCR infrastructure.

  public CCRMediator(PingState pingState)
  {
      _pingState = pingState;
  
      _dispatcher = new Dispatcher(0, "CCRMediator Threads");
      _dq = new DispatcherQueue("CCRMediator DispatcherQueue", _dispatcher);
      _dqExceptions = new DispatcherQueue("CCRMediator DispatcherQueue Exceptions", _dispatcher);
  }
  
  public void Start()
  {
      GatherPingRepliesModifyState();
  }

As you can see, there is not much to setting up the core CCR classes. Everything is given a name to aid in debugging. Passing zero to Dispatcher constructor instructs the class to create a thread on each Core of the computer.

I mentioned earlier that I wanted to give exceptions a higher priority. So I built a DispatcherQueue for exceptions only rather than mixing Exceptions among the other responses. Having a dedicated queue effectively puts exceptions to the top of the queue. As the Dispatcher executes in a round-robin, it will run as follows:

  • Check the Exception queue, execute the topmost item (if something exists),
  • Then move to the successful queue and execute the topmost item
  • Then move back to the Exception queue and so forth

Dispatchers and DispatcherQueues perform the execution. Configuring and initiating the execution process is done using Ports and Arbiters.





Page 2 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel