Search Wiki:

What is PCR?

Port Concurrency Runtime (PCR) is a Port-Based asyncronous message passing library.

Simply put, you push messages to ports and the messages are processed (i.e popped) asyncronously using handlers which we call Selectors. All messages (i.e. objects) are sent and received via a Port. Selectors are user defined message handlers that can atomically process a single message or multiple messages from one or more ports based on rules defined by the Selector. The library includes common selectors that should satisfy the most common patterns such as SelectOne, SelectMany, Choice, Interleave, and Join.

        /// <summary>
        /// Test single item selector. SelectOne "can" have a FIFO gaureentee (depending on other factors defined by usage). In this case, only one
        /// selector will be scheduled at a time, but can also be set to run concurrent. The selector will run again when a message is pushed on port.
        /// </summary>
        static void SelectOneTest()
        {
            // Create a port and add some items.
            Port<string> p = new Port<string>();
            for (int i = 0; i < 20; i++)
                p.Push(i.ToString("00"));
            int activeCount = 0;
 
            // Create and activate the selector.
            Selector.SelectOne<string>(true, p,
                delegate(string s)
                {
                    if ( Interlocked.Increment(ref activeCount) > 1 )
                        Console.WriteLine("Error: active count should never go above 1.");
                    Console.WriteLine("SelectOneTest: {0} ThreadID:{1:00} Active:{2}", s, Thread.CurrentThread.ManagedThreadId, activeCount);
                    Thread.Sleep(10);
                    Interlocked.Decrement(ref activeCount);
                }).Add(); 
        }


The above code shows the general pattern that will be used by all selectors. A port is created, messages are added, and we register a selector query to process messages when they arrive on the port. The PCR runtime handles invoking the selector(s) registered with the port based on the selector rules and message availability. So a selector could run right away or not at all. More complex patterns such as selecting from many ports (i.e. a Join) or selecting many messages from a single port follow the same pattern. Things like Where filters are as simple as supplying a predicate delegate to a selector.

Helpful Links

--
William Stacey [C# MVP]

Last edited Jan 30 2007 at 4:20 PM  by staceyw, version 33
Comments
Karloff wrote  Jan 29 2007 at 10:33 PM  
Excellent work and a much needed alternative to CCR for the community who may not be able to utilize commercial licensing of the MS Robotics Runtime components. I look forward to giving the PCR a run. Keep up the fantastic job and thanks!

staceyw wrote  Jan 30 2007 at 8:23 AM  
Thank you Karloff. I appreciate your kind words and encouragement. Made my day.
--William

adityapasumarthi wrote  Jul 14 2007 at 4:17 AM  
Hi,

I saw PCR recently and wanted the guys using PCR to take a look at my own concurrency libraries for .Net community. My libraries won Article of the Month award for Oct' 2006 and had clear design patterns for multi-threaded and asynchronous applications. These libraries come with lock-free implementations that provide good performance for server side applications. Moreover I tried to put pretty good documentation for these libraries, which are available as articles on CodeProject.com.

My intention of this post is only informational.

Here are the links to my articles and libraries.

1. Asynchronous Code Blocks
http://www.codeproject.com/cs/library/AsynchronousCodeBlocks.asp

2. Managed I/O Completion Ports (IOCP)
http://www.codeproject.com/cs/library/managediocp.asp

3. Managed I/O Completion Ports (IOCP) - Part 2
http://www.codeproject.com/cs/library/managediocp-part-2.asp

Regards,

Adityanand Pasumarthi

Updating...