C# Threadpool

Contents

What is C# Threadpool?

As we learned in previous chapter thread shutdown after its work is done which is a great thing, CLR clears the resource after thread shutdown and thus free up space for smooth program execution without you to write any code for thread management and garbage collection.

However,

Creation of thread is something that costs time and resource and thus will be difficult to manage when dealing with a large number of threads. Thread pool is used in this kind of scenario. When you work with thread pool from .NET you queue your work item in thread pool from where it gets processed by an available thread in the thread pool.

But,

After work is done this thread doesn’t get shutdown. Instead of shutting down this thread get back to thread pool where it waits for another work item. The creation and deletion of this threads are managed by thread pool depending upon the work item queued in the thread pool. If no work is there in the thread pool it may decide to kill those threads so they no longer consume the resources.

C# Thread Pool Queue

ThreadPool.QueueUserWorkItem is a static method that is used to queue the user work item in the thread pool. Just like you pass a delegate to a thread constructor to create a thread you have to pass a delegate to this method to queue your work.

Here is an example,

 1 using System;
 2 using System.Threading;
 3 
 4 class Example1
 5 {
 6     public static void Main()
 7     {
 8         // call QueueUserWorkItem to queue your work item
 9         ThreadPool.QueueUserWorkItem(Speak);
10 
11         Console.WriteLine("Press Enter to terminate!");
12         Console.ReadLine();
13     }
14 
15     //your custom method you want to run in another thread
16     public static void Speak(object stateInfo)
17     {
18         // No state object was passed to QueueUserWorkItem, so stateInfo is null.
19         Console.WriteLine("Hello World!");
20     }
21 }

as you can see we can directly pass this Speak method name to the QueueUserWorkItem method as it takes WaitCallback delegate as a parameter.

Here is the definition of this delegate,

1 public delegate void WaitCallback(object state);

See how it share the same signature like our Speak method with void as return type and take object as parameter.

QueueUserWorkItem also has overload for parameterised method like this,

1 QueueUserWorkItem(WaitCallback, Object)

Here the first parameter is your method name and the second parameter is the object that you want to pass to your method.

Here is an example,

 1 using System;
 2 using System.Threading;
 3 
 4 class Example1
 5 {
 6     public static void Main()
 7     {
 8         // call QueueUserWorkItem to queue your work item
 9         ThreadPool.QueueUserWorkItem(Speak, "Hello World!");
10 
11         Console.WriteLine("Press Enter to terminate!");
12         Console.ReadLine();
13     }
14 
15     //your custom method you want to run in another thread
16     public static void Speak(object s)
17     {
18         string say = s as string;
19         Console.WriteLine(say);
20     }
21 }

Did you notice how we passed our required parameter for Speak method to QueueUserWorkItem as the second parameter.

Limitations To Thread Pool Queue

ThreadPool.QueueUserWorkItem is really easy way to schedule your work into thread pool however it has its limitation, like you cannot tell whether a particular work operation is finished and also it does not return a value.

However,

A Task is something that you can use in place of ThreadPool.QueueUserWorkItem. It tells whether an operation is completed and also returns a value after the task is completed. For more information, see my blog post C# Task

In the next post C# Race Condition, we’ll learn what is a Race Condition in a multithreaded program and how much it is critical to synchronize a multithreaded program having Shared Resources.

Further Reading

  • C# Race Condition - This article explains what is Race Condition and Shared Resources in a multithreaded program and how much it is critical to synchronize a multithreaded program having shared resources. Thread Synchronization is a mechanism which ensures that two or more concurrent process or threads do not execute some particular section of program especially critical section at the same time.

  • C# Task - C# Task is one of the central elements of the task-based asynchronous pattern first introduced in the .NET Framework 4. This article serves to be a quick guide for using Tasks in C#. It discusses different methods to create tasks and handle their execution.

C# Threadpool
Share this

Subscribe to Code with Shadman