System.Threading Namespace

Provides classes and interfaces that enable the creation and management of threads and synchronization primitives. This namespace is fundamental for building responsive and scalable applications by allowing concurrent execution of tasks.

Key Types

Thread Class

Represents a thread of execution in the common language runtime (CLR).

Description

Use the Thread class to create and manage threads. You can start a thread, set its priority, and interrupt it. Threads are essential for performing long-running operations in the background without blocking the main application thread.

Key Members

  • Thread(ThreadStart): Constructor to create a new thread.
  • Thread(ParameterizedThreadStart): Constructor to create a thread with a parameter.
  • Start(): Starts the execution of the thread.
  • Join(): Waits for the thread to terminate.
  • Sleep(int): Suspends the current thread for the specified number of milliseconds.
  • CurrentThread: Gets the currently executing thread.

Ensures that a block of code is executed by only one thread at a time.

Description

The lock statement is a convenient syntax for acquiring the monitor lock for a given object, executing a statement block, and then releasing the lock. It's used to prevent race conditions in multithreaded applications.

Example

lock (this)
{
    _counter++;
}
Monitor Class

Provides basic synchronization primitives that synchronize access to objects.

Description

The Monitor class provides static methods for controlling multithreaded access to objects. It's the underlying mechanism used by the lock statement.

Key Members

  • Enter(object): Enters a monitor lock for the specified object.
  • Exit(object): Exits a monitor lock (releasing the object that was entered).
  • Wait(object): Causes the current thread to wait until another thread invokes the Pulse or PulseAll method for the specified object.
  • Pulse(object): Notifies one waiting thread that can proceed.
  • PulseAll(object): Notifies all waiting threads that can proceed.
Mutex Class

Represents a thread-safe way to access a shared resource.

Description

A Mutex is a synchronization primitive that can be used to control access to a shared resource in a multithreaded application. It can be used across application domains and even across processes.

Key Members

  • WaitOne(): Attempts to acquire the mutex.
  • ReleaseMutex(): Releases the mutex.
Semaphore Class

Restricts the number of concurrent calls to a collection of resources.

Description

A Semaphore limits the number of threads that can access a shared resource concurrently. It maintains a count of available slots.

Key Members

  • WaitOne(): Decrements the semaphore count, blocking if necessary until a slot is available.
  • Release(): Increments the semaphore count, potentially releasing a waiting thread.

Signals one or more threads that a change has occurred.

Description

AutoResetEvent is a synchronization primitive that can be used to signal when an event has occurred. It has two states: signaled and non-signaled. When signaled, one waiting thread is released. When non-signaled, threads block until the event is signaled.

Key Members

  • WaitOne(): Blocks the current thread until the current AutoResetEvent is signaled.
  • Set(): Sets the state of the event to signaled, releasing one or more waiting threads.
  • Reset(): Sets the state of the event to non-signaled.

Signals one or more threads that an event has occurred.

Description

Similar to AutoResetEvent, but ManualResetEvent remains signaled until Reset() is called. This allows multiple threads to be released when the event is signaled.

Key Members

  • WaitOne(): Blocks the current thread until the current ManualResetEvent is signaled.
  • Set(): Sets the state of the event to signaled.
  • Reset(): Sets the state of the event to non-signaled.