Reader Level:
ARTICLE

Changing the default limit of 25 threads of ThreadPool Class

Posted by Yash Malge Articles | Multithreading in C# June 19, 2003
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 then here you go.
  • 0
  • 0
  • 24320
Download Files:
 

1. 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 ThreadPool class per processor. But believe me its 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 then here you go.

2. Include header file

The first problem which I faced as being a VC++ developer is how to include "mscoree.h" header file in C# project. Basically, we can not include header files in C# project because C# is purely object oriented programming language. The header files are full of constants, which wouldnt fit well anyway in this.

a. 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 a generic functions.

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

b. 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 that the programming model of COM and .Net differs greatly. The differences are too great to cover in detail right here. Hence, Its pretty obvious that some form of "Difference Manager" is needed. Thats where COM interop comes into picture. Because of the COM interop, COM objects become usable from .Net object. COM Interop provides access to existing COM components without requiring that the original component be modified.

c. How to use C# to interoperate with COM objects and its interfaces defined in Mscoree.h file?

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

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

There are the following steps to create the COM interop for the mscoree.h file and putting all together.

1. 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 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, TlbImp.exe utility cannot handle the definitions in the mscoree typelib. Hence, (I guess) the only one alternative is available i.e. to manually define the COM definitions in C# source code using C# attributes. Once we create the C# source mapping, then we can simply compile the C# source code to produce the managed wrapper for the COM objects defined in the mscoree.h file.

2.  Declaring a COM coclass

The COM coclass is a COM object. The coclass definition in type library enables 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 "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 class ThreadManager 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.

3. 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 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 in order to query/control the CLR-managed thread pool. Because we are interested in adjusting 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 "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(); // Dont call. In correct //signature
void UnregisterWait(); // Dont call. In correct signature
void QueueUserWorkItem(); // Dont call. In correct signature
void CreateTimer(); // Dont call. In correct signature
void ChangeTimer(); // Dont call. In correct signature
void DeleteTimer(); // Dont call. In correct signature
void BindIoCompletionCallback(); // Dont call. In correct // signature
void CallOrQueueUserWorkItem(); // Dont call. In correct //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.

4. Creating a COM Object

COM coclasses are represented in C# as classes with a parameter less 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();
:
:
}

5. 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, then an invalid cast exception is thrown.

The cast is required since interop shims like 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;

6. Get Max Thread Count

The GetMaxThreads and SetMaxThreads methods can be called using above ICorThreadpool interface object ct as shown bellow.

Get Max Thread Count

uint maxWorkerThreads;
uint maxIOThreads;
ct.GetMaxThreads(
out maxWorkerThreads, out maxIOThreads);

If ICorThreadPool.GetMaxThreads returns 25 and 25, then 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 all together

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

This program uses 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 GetMaxThreads method of ICorThreadPool interface. Then it sets the max thread count using SetMaxThreads method of 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 GetMaxThreads method. This time it should display the same value which being set by SetMaxThreads method.

The main purpose of this article is not to test 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..

COMMENT USING

Trending up