VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-multithreading/

⇱ C# Multithreading - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# Multithreading

Last Updated : 20 Apr, 2026

Multithreading in C# is a technique that allows a program to perform multiple operations at the same time by executing different threads concurrently. It is commonly used to improve performance, responsiveness, and efficient resource utilization in applications. C# provides multithreading support through the System. Threading namespace and higher-level abstractions like Task Parallel Library (TPL) and async/await.

A thread is the smallest unit of execution in a program. Every C# program starts with a main thread created automatically by the runtime. You can create additional threads to run tasks in parallel with the main thread.

Creating Threads in C#

C# provides multiple ways to create threads:

1. Using Thread Class

The Thread class in the System. Threading namespace allows explicit creation and management of threads. This is the most basic way of working with multithreading.

Output:

Worker Thread: 1

Main Thread: 1

Worker Thread: 2

Main Thread: 2

...

Explanation:

  • Thread object is created with a target method.
  • Start() begins execution in parallel with the main thread.

2. Using ParameterizedThreadStart

Sometimes threads need input data. C# provides the ParameterizedThreadStart delegate to pass parameters to threads.

Output:

Message: Hello from Thread

3. Using Lambda Expressions

Instead of separate methods, you can define thread logic inline.

Output:

Lambda Thread: 1

Lambda Thread: 2

Lambda Thread: 3

Foreground and Background Threads

C# threads can run in two modes:

  • Foreground Threads: Keep running until they finish, even if the main thread ends.
  • Background Threads: Terminate when all foreground threads end.

If the main thread ends before the background thread completes, the background thread is terminated.

Thread Synchronization

When multiple threads access and modify shared data, issues like race conditions can occur. This happens when two or more threads try to update the same variable at the same time, leading to inconsistent results.

To solve this, C# provides synchronization mechanisms. The most common is the lock statement, which ensures that only one thread can access a critical section of code at a time.

Example with Lock:

  • The lock keyword ensures that only one thread at a time can enter the Increment() method.
  • Without lock, both threads might try to update count simultaneously, causing incorrect results.

Thread Pooling

Creating and destroying threads repeatedly consumes resources. To optimize this, C# provides a Thread Pool, which is a collection of worker threads managed by the runtime. The pool reuses threads for multiple tasks, reducing overhead.

Output (order may vary):

ThreadPool: Task 1

ThreadPool: Task 2

Multithreading with Tasks

The Task Parallel Library (TPL) provides a modern and easier way to manage multithreading. It abstracts low-level thread management and is highly optimized.

Output (interleaved):

Task 1: 1

Task 2: 1

Task 1: 2

Task 2: 2

...

Async and Await (Asynchronous Programming)

For I/O-bound operations (like file access, database calls, or API requests), C# provides async and await keywords. These make asynchronous code look like synchronous code, improving readability.

Output:

Fetching...

Data fetched!

Done!

Key Points

  • A thread is a lightweight execution unit inside a process.
  • Multiple threads can run in parallel to improve responsiveness and efficiency.
  • Foreground threads keep running until completion, background threads terminate when all foreground threads have completed execution..
  • Synchronization mechanisms like lock prevent race conditions when threads share data.
  • Use ThreadPool, Task Parallel Library, or async/await for modern, efficient multithreading.
Comment
Article Tags:

Explore