System.Threading Namespace

Provides classes and interfaces that support the creation and management of threads, and other types that manage concurrent operations. This namespace is fundamental for writing multithreaded applications in .NET.

Overview

The System.Threading namespace is essential for any application that needs to perform operations concurrently. It allows you to execute multiple parts of your program simultaneously, improving responsiveness and performance. Key concepts include creating and managing threads, using synchronization primitives to prevent race conditions, and leveraging the Task Parallel Library (TPL) for higher-level concurrency abstractions.

Classes

Thread Class

Represents an operating system thread, which is a single sequential flow of control.

public class Thread : System.Runtime.ConstrainedExecution.CriticalFinalizerObject, System.IDisposable
The Thread class allows you to create, start, stop, and manage threads. It provides properties to control thread priority, culture, and apartment state.

Task Class

Represents an asynchronous operation.

public class Task : System.IAsyncResult, System.IDisposable
The Task class, introduced with the Task Parallel Library (TPL), provides a more modern and flexible approach to asynchronous programming compared to traditional threading models. It simplifies common asynchronous patterns.

ThreadPool Class

Provides a pool of threads that can be used to execute tasks.

public static class ThreadPool
Using the ThreadPool is generally more efficient than creating individual threads, as it reuses threads to execute tasks, reducing the overhead of thread creation and destruction.

ManualResetEvent Class

Signals one or more threads that an event has occurred.

public class ManualResetEvent : System.Threading.WaitHandle, System.IDisposable
A ManualResetEvent is a synchronization primitive that can be explicitly reset. Threads can wait for the event to become signaled.

AutoResetEvent Class

Signals one or more threads that an event has occurred. The event is automatically reset after the wait is released.

public class AutoResetEvent : System.Threading.WaitHandle, System.IDisposable
Unlike ManualResetEvent, an AutoResetEvent automatically resets itself to the nonsignaled state once a waiting thread is released.

Semaphore Class

Constrains the number of threads that can access a shared resource or perform an action concurrently.

public class Semaphore : System.Threading.WaitHandle, System.IDisposable
A Semaphore limits the number of concurrent accesses to a resource. Threads must increment the semaphore count to enter a protected section and decrement it upon exit.

Mutex Class

Represents a mutual-exclusion synchronization primitive.

public class Mutex : System.Threading.WaitHandle, System.IDisposable
A Mutex is used to ensure that only one thread can access a particular section of code or resource at any given time, preventing conflicts in multithreaded environments.

CancellationTokenSource Class

Propagates a notification that operations should be canceled.

public class CancellationTokenSource : System.IDisposable
This class is part of the cooperative cancellation pattern, allowing an operation to be signaled to stop its work gracefully.

Interfaces

  • IAsyncResult

    Defines a standard structure for asynchronous operations.

    public interface IAsyncResult
  • IPromise

    Represents a future result of an asynchronous operation.

    public interface IPromise<TResult>