Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Thread-Safe Locking: Choosing the Right Synchronization

Learn how to protect shared state in C# with lock, Monitor, Mutex, SemaphoreSlim, and ReaderWriterLockSlim, and avoid common concurrency pitfalls.

~5 min readC#
#C##Thread Safety#Locking#Concurrency
Illustration of C# thread safe lock concept showing multiple threads accessing a locked shared resource.

When multiple threads read and write the same data, the order of operations is not guaranteed. A race condition can corrupt state or throw exceptions. In C#, the lock statement is the most direct way to make a block of code thread safe. This article explains how common C# synchronization primitives work, compares the lock statement with other options, and gives practical guidance on choosing the right one.

The Race Condition That Makes Locking Necessary

Consider a counter that is incremented from multiple threads:

csharp
int counter = 0; void Increment() => counter++;

The ++ operation is not atomic. It reads the current value, adds one, and writes it back. If two threads execute this simultaneously, one increment can be lost. The result may be lower than expected. This is a classic race condition. To prevent it, you need to ensure that the read-modify-write sequence is performed by only one thread at a time. A lock provides that mutual exclusion.

The lock Statement: The Simplest Thread-Safe Lock

The lock keyword in C# is the simplest way to protect a critical section. It acquires a mutual-exclusion lock on a specified object, executes the block, and releases the lock even if an exception occurs. The syntax is:

csharp
private readonly object _lockObject = new object(); void Increment() { lock (_lockObject) { counter++; } }

The lock statement is based on Monitor.Enter and Monitor.Exit, with the lock released even if the body throws. The object passed to lock must be a reference type, and it is used as the synchronization token. It is recommended to use a dedicated private object rather than this or a Type object, because those can be locked by unrelated code, leading to unexpected contention or deadlocks.

What Happens Under the Hood: Monitor and the lock Statement

The lock statement uses Monitor.Enter to acquire the lock and Monitor.Exit to release it. The runtime keeps track of which thread holds the lock. If another thread attempts to enter the lock, it blocks until the lock is released. The Monitor class also provides TryEnter with a timeout, which lock does not expose directly. If you need a non-blocking attempt or a timeout, you must use Monitor.TryEnter explicitly.

csharp
if (Monitor.TryEnter(_lockObject, TimeSpan.FromMilliseconds(100))) { try { // protected code } finally { Monitor.Exit(_lockObject); } }

The lock statement is sufficient for most cases, but Monitor.TryEnter gives you more control over waiting behavior.

Beyond lock: Mutex, Semaphore, and ReaderWriterLockSlim

The lock statement is not the only synchronization mechanism in C#. Depending on your scenario, you might need a different primitive.

  • Mutex is a cross-process lock. It can be used to synchronize threads across multiple processes on the same machine. It is heavier than lock because it involves operating system kernel objects.
  • Semaphore and SemaphoreSlim control access to a resource pool with a fixed count. They allow a specified number of threads to enter the critical section simultaneously. SemaphoreSlim is a lightweight version for in-process use.
  • ReaderWriterLockSlim allows multiple readers to enter concurrently, but only one writer at a time. This is useful when reads are far more frequent than writes and you want to minimize contention.

Here is a comparison table:

PrimitiveScopeConcurrencyTypical Use Case
lock / MonitorIn-processExclusiveProtecting short critical sections
MutexCross-processExclusiveCoordinating between processes
SemaphoreSlimIn-processCountedLimiting concurrent resource usage
ReaderWriterLockSlimIn-processMultiple readers / one writerRead-heavy workloads

Choosing the Right Primitive for the Job

The lock statement is the default choice for most in-process mutual exclusion. It is fast, simple, and safe when used correctly. Use ReaderWriterLockSlim when you have a shared resource that is read frequently and written rarely, and when the read operation is expensive enough to justify the added complexity. Use SemaphoreSlim when you need to limit the number of concurrent operations, such as throttling database connections. Use Mutex only when you need to synchronize across processes; otherwise, its overhead is unnecessary.

A common mistake is to use lock for a long-running operation, such as I/O. Holding a lock during I/O blocks other threads for the entire duration, which can cause severe contention and performance degradation. Prefer to keep critical sections short and avoid I/O inside them.

Deadlocks and Lock Ordering

A deadlock occurs when two threads each hold a lock and wait for the other's lock. For example, thread A locks object X then tries to lock Y, while thread B locks Y then tries to lock X. Neither can proceed. To avoid deadlocks, you must ensure consistent lock ordering across all threads. If every thread acquires locks in the same global order, a cycle cannot occur. If you cannot enforce that, Monitor.TryEnter with a timeout lets a thread detect that a lock is unavailable and back off instead of blocking indefinitely.

csharp
// Consistent order: always lock _lockA before _lockB lock (_lockA) { lock (_lockB) { // safe } }

Performance and Contention: What Actually Matters

The cost of a lock is not zero. An uncontended lock has relatively small overhead, but the real cost appears under contention. When multiple threads wait for the same lock, they block and the operating system must wake them, which is expensive. High contention can degrade throughput dramatically. Therefore, the goal is to reduce the time a lock is held and to minimize the number of threads competing for it.

ReaderWriterLockSlim can improve performance in read-heavy scenarios because it allows multiple readers to proceed concurrently. However, it has a higher overhead than a simple lock for a single writer or when the critical section is very short. Always measure the actual behavior of your application before optimizing.

Common Pitfalls and How to Avoid Them

One frequent mistake is locking on a public type or a string literal. For example, lock ("some string") can cause unexpected contention because other code might use the same object for locking. Always use a private, readonly instance object.

Another issue is forgetting that lock is reentrant. The same thread can acquire the same lock multiple times without deadlocking, because Monitor tracks the owning thread and a recursion count. This is usually desirable, but it can mask design problems if you are not careful.

Finally, be aware of async code. Although lock can be used in an async method when the protected block contains no await, C# does not allow await inside a lock body, so you cannot hold the lock across an asynchronous operation. For that scenario, use SemaphoreSlim with WaitAsync or another async-aware synchronization primitive.

When to Avoid Locking Altogether

In some cases, you can avoid locks entirely by using atomic operations or immutable data. The Interlocked class provides atomic operations for integers, such as Increment, Decrement, and Add. For a simple counter, Interlocked.Increment avoids taking a lock and is more direct. Similarly, using immutable collections or functional patterns can eliminate the need for locking by ensuring that shared state is never modified.

csharp
Interlocked.Increment(ref counter);

This is not always applicable, but it is worth considering when the critical section is a single operation that has an atomic equivalent.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.