The series so far:

  1. The Unconventional Guide to Introduction to Thread
  2. Why Do People Think Dedicated CLR Threads is a Good Idea?
  3. How Not Knowing Thread Members and Execution State Makes You a Rookie
  4. Doing Thread Scheduling and Priority the Right Way
  5. How to Start Using CLR's Thread Pool
  6. What Wikipedia Can't Tell You About Thread Execution Contexts
  7. The Insider's Guide to Cooperative Cancellation and Timeout

Have they ever tried to introduce CLR logical threads?

In early days, CLR team felt they would have logical threads, which did not map to Windows Threads. They failed to do that around the year 2005 and gave up on it. Still, we see some code portions which are residues of that attempt.
For example:

  • System.Environment exposes a CurrentManagedThreadId property to return CLR Id for Thread.
  • System.Diagnostics.ProcessThread class exposes an Id property to returns Windows’s ID.

CLR and Windows Threads

  1. CLR thread directly maps to Windows thread and make use of Windows thread internally.
  2. Microsoft is enhancing CLR threads by making it lightweight, optimal use of resources and so on.
  3. So it is always advisable to use CLR threads instead of using Windows threads.


How to create a thread and have it perform an asynchronous compute-bound operation.

Typically, you’d want to create a dedicated thread if you’re going to execute code that requires the thread to be in a particular state that is not normal for a thread pool thread.

Compute-Bound operation
In computer science, a computer is CPU-bound (or compute-bound) when the time for it to complete a task is determined principally by the speed of the central processor: processor utilisation is high, perhaps at 100% usage for many seconds or minutes.

When to use Dedicated Thread?

  1. Non-normal thread priority. All thread pool threads run at normal priority.
  2. Foreground thread, prevent the application from dying. Thread pool threads are always Background threads. We cover in detail in the future posts.
  3. The compute-bound task is hugely long-running, thread pool’s logic tries to figure out whether to create an additional thread.
  4. You want to start a thread and possibly abort it prematurely by calling Thread’s Abort method

How to create Dedicated Thread?

  1. To create a dedicated thread, you construct an instance of the System.Threading.Thread class, passing the name of a method into its constructor. Here is the prototype of Thread’s constructor.
// The thread class is in System.Threading namespace
public sealed class Thread : CriticalFinalizerObject, ... { 
   public Thread(ParameterizedThreadStart start); 
   // Less commonly used constructors are not shown here   
      // Thread also offers a constructor that accepts no inputs and returns void
  1. The start parameter identifies the method that the dedicated thread executes, and this method must match the signature of the ParameterizedThreadStart delegate.

delegate void ParameterizedThreadStart(Object obj);

Points to Ponder

  1. Constructing a Thread object is a relatively lightweight operation because it does not create a physical operating system thread.
  2. To create the operating system thread and have it start executing the callback method, you must call Thread’s Start method, passing into it the object (state), passed down as the callback method’s argument.

Demo - Single Dedicated Thread


using System;
using System.Threading;

namespace DedicatedThread
    class Program
        static void Main(string[] args)
            Thread t = new Thread(WriteY); // Kick off a new thread
            t.Start(); // running WriteY()
                       // Simultaneously, do something on the main thread.
            for (int i = 0; 
                i < 1000; i++) Console.Write("x");

        static void WriteY()
            for (int i = 0; i < 1000; i++)

Reasons to use Threads

There are three primary reasons for using Threads

  1. Responsiveness (typically for client-side GUI applications)
    Windows gives each process its thread so that one application entering an infinite loop doesn’t prevent the user from working with other applications. Similarly, within your client-side GUI application, you could spawn some work off onto a thread so that your GUI thread remains responsive to user input events. In this example, you are possibly creating more threads than available cores on the machine, so you are wasting system resources and hurting performance. However, the user is gaining a responsive user interface and therefore having a better overall experience with your application.
  2. Performance (for client and server side applications)
    Because Windows can schedule one thread per CPU and because the CPUs can execute these threads concurrently, your application can improve its performance by having multiple operations executing at the same time in parallel. Of course, you only get the improved performance if and only if your application is running on a machine with multiple CPUs in it. Today, machines with multiple CPUs in them are entirely conventional, so designing your application to use multiple cores makes sense and is the focus of next sessions.
  3. CPU Utilization
    All CPUs in the computer is running at 100 percent
    • Drains Battery Faster
    • Causes Heating Problems

Nowadays computers ship with tremendous computing power. Hence, hardware manufacturers have a hard time selling multi-core machines