Windows Server 2008 Build Scalable applications

Today I found some features of Windows Server 2008 that can be used in the application to build scalable and more reliable applications and I added summary of my learning to my blog.

Thread enhancements

In earlier releases, applications can have a single thread pool per process. Windows Server 2008 allows applications to create multiple thread pools per process, with each pool scheduled independently.  The new thread pool APIs also support creation of  a new thread pool, set the maximum and minimum number of threads managed by a thread pool, free thread pool, canceling pending work items, and simplified cleanup of resources.

I have build C++ application where there is master thread and a set of worker threads managed by master thread. In earlier releases, this master-slave concept was not available in .NET thread pool and all the threads of thread pool were the same. Windows 2008 allows Thread Ordering Service where each thread ordering group has a parent thread and zero or more client threads. A thread becomes the parent thread by creating a thread ordering group.

Wait Chain Traversal(WCT)

We all know the use of synchronization objects, such as critical sections, to control access to shared resources in application using threads. Still we face race conditions and deadlock states in threaded applications. For example, consider an application with two threads (A and B) and two synchronization objects (1 and 2). If Thread A has Object 1 and is blocked waiting for Object 2, while Thread B has Object 2 and is blocked waiting for Object 1, the threads are said to be deadlocked.

Wait chain traversal (WCT) is a mechanism for debugging blocked threads and processes and detecting deadlocks. WCT allows debugging software to detect problems both within and across process boundaries. Using WCT, Debugging software first identifies the processes and threads to analyze and then reports the state of threads in one or more processes. . A wait chain is an alternating directed graph of threads and synchronization objects. Some guidelines for WCT is as follows

  • A thread’s state (unblocked, blocked, or deadlocked) is reported as a wait chain.
  • The first node in a wait chain is the thread being analyzed.
  • An edge from a thread to an object indicates that the thread is waiting for the object;
  • An edge from an object to a thread indicates that the thread is the current owner of the object.
  • The simplest wait chain reflects a thread that is not blocked. It is composed of a single node, representing the unblocked thread.
  • A blocked thread is represented by a wait chain containing multiple nodes that is non-cyclic: that is, there are no two nodes in the chain that represent the same thread or object.
  • When a thread is deadlocked, the wait chain that represents it is cyclic.

Consider the scenario where Thread A is blocked waiting for a mutex object that is owned by Thread B. WCT looks as follows: Thread A → Mutex 1 → Thread B. Consider the scenario where Thread A owns Object 1 and is blocked waiting for Object 2, while Thread B owns Object 2 and is blocked waiting for Object 1. WCT looks as follows: Thread A → Object 2 → Thread B → Object 1 → Thread A