Welcome to Multithreading Documentation
The .NET Framework provides a rich set of classes and patterns to create robust multithreaded applications. This guide covers fundamental concepts, the Thread
class, the Task Parallel Library (TPL), async programming, and synchronization mechanisms.
Key Concepts
- Thread: Represents an OS thread.
- Task: Represents an asynchronous operation, managed by the TPL.
- Async/Await: Language constructs that simplify asynchronous code.
- Synchronization: Tools such as
lock
,Mutex
,SemaphoreSlim
, andConcurrentCollection
.
Getting Started
Explore the topics on the left to dive deeper. Below is a quick example of creating and starting a thread:
using System;
using System.Threading;
class Program{
static void Main(){
Thread t = new Thread(Worker);
t.Start();
Console.WriteLine("Main thread continues...");
}
static void Worker(){
Console.WriteLine("Worker thread is running.");
}
}
For modern code, consider using Task.Run
:
using System;
using System.Threading.Tasks;
class Program{
static async Task Main(){
await Task.Run(()=> DoWork());
Console.WriteLine("Work completed.");
}
static void DoWork(){
// Simulate work
Thread.Sleep(1000);
}
}
Explore the API Index for the full list of classes and methods.