Changing the Default Limit of 25 Threads of the ThreadPool Class

Environment: Threading

Introduction

According to the Microsoft documentation, "The thread pool is created the first time you create an instance of the ThreadPool class. The thread pool has a default limit of 25 threads per available processor, which could be changed using CorSetMaxThreads as defined in the mscoree.h file."

It appears to be a simple function call to change the default thread limit of 25 threads of the ThreadPool class per processor. But, believe me, it's not that easy at all. I have found the way to do this. But however, if you need more than 25 threads, I would seriously question the architecture of the application. The ThreadPool is useful for managing threads that are usually in a wait state and that take only a short amount of time to do their work. If still you would like to change the default limit of 25 threads, here you go.

Include Header File

The first problem that I faced being a VC++ developer is how to include a "mscoree.h" header file in a C# project. Basically, we cannot include header files in a C# project because C# is purely an object-oriented programming language. The header files are full of constants, which wouldn't fit well in this, anyway.

What Is the mscorre.h File?

Basically, MSCoree.dll is the Microsoft .NET Runtime Execution Engine, which hosts the .NET CLR (Common Language Runtime) in an unmanaged environment, and exposes generic functions.

Then, the main question that remains is, "How do you call unmanaged code (Mscoree.dll functions) from managed code (C# application)? The only way to do this is to interop out to unmanaged code and call the unmanaged interface method to increase the max thread count.

What Is the Interop?

The .NET managed applications can leverage existing COM components. The COM components inter-operate with the .NET runtime through an interop layer that will handle all the plumbing between translating messages that pass back and forth between managed runtime and the COM component operating in the unmanaged realm, and vice versa. Well, as you probably know, the programming model of COM and .NET differs greatly. The differences are too great to cover in detail right here. Hence, it's pretty obvious that some form of "Difference Manager" is needed. That.s where COM interop comes into the picture. Because of the COM interop, COM objects become usable from .NET objects. COM interop provides access to existing COM components without requiring that the original component be modified.

How to Use C# to Interoperate with COM Objects and Their Interfaces Defined in a Mscoree.h File

C# uses .NET Framework facilities to perform COM Interop. C# has support for:

  • Creating COM objects
  • Determining whether a COM interface is implemented by an object
  • Calling methods on COM interfaces
  • Implementing objects and interfaces that can be called by COM clients

Here are the steps to create the COM interop for the mscoree.h file and put it all together.

  1. Create a COM Class Wrapper.
  2. Declare a COM Coclass.
  3. Declare a COM Interface.
  4. Create a COM Object.
  5. Use Casts Instead of QueryInterface.
  6. Set Max Thread Count.

At first, add an empty C# code file called "ICordThread,cs" to the C# project.

Creating a COM Class Wrapper

In our C# code, to reference COM objects and interfaces defined in Mscoree.h file, we need to include a .NET Framework definition for the COM interfaces in our C# build. According to the Microsoft documentation, "The easiest way to do this is to use TlbImp.exe (Type Library Importer), a command-line tool included in the .NET Framework SDK. TlbImp converts a COM type library into .NET Framework metadata, effectively creating a managed wrapper that can be called from any managed language. .NET Framework metadata created with TlbImp can be included in a C# build via the /R compiler option."

Unfortunately, the TlbImp.exe utility cannot handle the definitions in the mscoree typelib. Hence, (I guess) the only one alternative is available—to manually define the COM definitions in the C# source code using C# attributes. Once we create the C# source mapping, we can simply compile the C# source code to produce the managed wrapper for the COM objects defined in the mscoree.h file.

Declaring a COM Coclass

The COM coclass is a COM object. The coclass definition in the type library enables you to list the interfaces and attributes of a COM object.

The COM coclasses are represented in C# as classes. These classes must have the ComImport attribute associated with them. The following restrictions apply to these classes:

  • The class must not inherit from any other class.
  • The class must implement no interfaces.
  • The class must also have a Guid attribute that sets the globally unique identifier (GUID) for the class.

Add the following declaration of coClass to the "ICordThread.cs" file:

// Declare ThreadManager as a COM coclass:

[
  // CLSID_CorRuntimeHost from MSCOREE.DLL
Guid("CB2F6723-AB3A-11D2-9C40-00C04FA30A3E"),ComImport
]

class ThreadManager    // Cannot have a base class or interface
                       // list here.
  {
  // Cannot have any members here
  // NOTE that the C# compiler will add a default constructor
  // for you (no parameters).
  }

The ComImport attribute marks the class as an externally implemented COM class. Such a class declaration enables the use of a C# name to refer to a COM class.

The above code declares a ThreadManager class as a class imported from COM that has a CLSID of "CB2F6723-AB3A-11D2-9C40-00C04FA30A3E". Instantiating a ThreadManager instance causes a corresponding COM instantiation.

Declaring a COM Interface

COM interfaces are represented in C# as interfaces with ComImport and Guid attributes. They cannot include any interfaces in their base interface list, and they must declare the interface member functions in the order that the methods appear in the COM interface.

COM interfaces declared in C# must include declarations for all members of their base interfaces with the exception of members of IUnknown and IDispatch. The .NET Framework automatically adds these.

By default, the .NET Framework provides an automatic mapping between the two styles of exception handling for COM interface methods called by the .NET Framework.

  • The return value changes to the signature of the parameter marked retval (void if the method has no parameter marked as retval).
  • The parameter marked as retval is left off of the argument list of the method.

Any non-success return value will cause a System.COMException exception to be thrown.

The following code shows a COM interface declared interface that was declared in C# (note that the methods use the COM error-handling approach).

The ICorThreadpool interface is documented (prototypes only) in mscoree.h, but is not made available from mscoree.tlb. So, the following interop stub lets us get our hands on the interface to query/control the CLR-managed thread pool. Because we are interested in adjusting thzae maximum thread count of the thread pool configuration, most of the members are actually invalid and cannot be called in their current form.

Add the following code to the "ICordThread.cs" file:

// derives from IUnknown interface:
[
// IID_IcorThreadPool
  Guid("84680D3A-B2C1-46e8-ACC2-DBC0A359159A"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
]

public interface ICorThreadpool        // Cannot list any base
                                       // interfaces here
{
// Note that IUnknown Interface members are NOT listed here:

void RegisterWaitForSingleObject();    // Don't call.
                                       // Incorrect signature

void UnregisterWait();                 // Don't call.
                                       // Incorrect signature

void QueueUserWorkItem();              // Don't call.
                                       // Incorrect signature

void CreateTimer();                    // Don't call.
                                       // Incorrect signature

void ChangeTimer();                    // Don't call.
                                       // Incorrect signature

void DeleteTimer();                    // Don't call.
                                       // Incorrect signature

void BindIoCompletionCallback();       // Don't call.
                                       // Incorrect signature

void CallOrQueueUserWorkItem();        // Don't call.
                                       // Incorrect signiture

void SetMaxThreads( uint MaxWorkerThreads,
                    uint MaxIOCompletionThreads );

void GetMaxThreads( out uint MaxWorkerThreads,
                    out uint MaxIOCompletionThreads );

void GetAvailableThreads( out uint AvailableWorkerThreads,
                          out uint AvailableIOCompletionThreads );

}

Note how the C# interface has mapped the error-handling cases. If the COM method returns an error, an exception will be raised on the C# side.

Creating a COM Object

COM coclasses are represented in C# as classes with a parameterless constructor. Creating an instance of this class using the new operator is the C# equivalent of calling CoCreateInstance. Using the class defined above, it is simple to instantiate the ThreadManager class:

public static void Main()
    {
        //
        // Create an instance of a COM coclass - calls
        //
        // CoCreateInstance(CB2F6723-AB3A-11D2-9C40-00C04FA30A3E,
        //                  NULL, CLSCTX_ALL,
        //                  IID_IUnknown, &f)
        //
        // returns null on failure.
        //

MSCoreeTypeLib.ThreadManager threadManager = new
                             MSCoreeTypeLib.ThreadManager();
    }

Using Casts Instead of QueryInterface

A C# coclass is not very useful until you can access an interface that it implements. In C++, you would navigate an object's interfaces using the QueryInterface method on the IUnknown interface. In C#, you can do the same thing by explicitly casting the COM object to the desired COM interface. If the cast fails, an invalid cast exception is thrown.

The cast is required because interop shims such as CorRuntimeHost cannot have methods, which would be required if it were to advertise that it implements ICorThreadPool statically.

MSCoreeTypeLib.ThreadManager threadManager = new
                             MSCoreeTypeLib.ThreadManager();
// QueryInterface for the ICorThreadPool interface:
MSCoreeTypeLib.ICorThreadpool ct =
               (MSCoreeTypeLib.ICorThreadpool)threadManager;

Get/Set Max Thread Count

The GetMaxThreads and SetMaxThreads methods can be called by using the above ICorThreadpool interface object ct, as shown below.

Get Max Thread Count
uint maxWorkerThreads;
uint maxIOThreads;
ct.GetMaxThreads(out maxWorkerThreads, out maxIOThreads);

If ICorThreadPool.GetMaxThreads returns 25 and 25, that's a total of 50 threads (as opposed to saying there are 25 threads max, of which up to 25 can be devoted to I/O )

Set Max Thread Count

maxWorkerThreads = 35;
maxIOThreads     = 35;
ct.SetMaxThreads(maxWorkerThreads, maxIOThreads);

Putting It All Together

This sample program demonstrates how to change the max thread count for the CLR-managed thread pool at runtime.

This program uses the COM interop to reach out and call MSCOREE.

This program takes advantage of an interface called ICorThreadpool that is implemented by the runtime. Because this interface is mentioned in mscoree.h, but not documented in mscoree.tlb, an explicit interop shim is used. See ICorThreadPool.cs in this project for details.

At first, this application gets and displays the max thread count using the GetMaxThreads method of the ICorThreadPool interface. Then, it sets the max thread count using the SetMaxThreads method of the IcorThreadPool interface.

It starts the 10 threads using .NET Thread pool object. At the end of the code, it again gets the max thread count using the GetMaxThreads method. This time, it should display the same value that was set by the SetMaxThreads method.

The main purpose of this article is not to test the ThreadPool class or its functionality. The only purpose of this application is to show how to adjust the max thread count of the ThreadPool class.

References

About the Author

Yash Malge began his professional career as a software engineer around seven years ago and received various opportunities to work on hi-tech projects using the latest tools and technology, especially in the telecommunications industry. Eventually, he became a software consultant and has been providing services for companies such as Motorola, Lucent, Broadwing (Cincinnati Bell), Lexmark, IBM, and Atos Origin.

Contact: yash@hotmail.com

Downloads

Download source code - 8 Kb


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds