C# Monitor vs Lock: When to Use Each
The lock statement compiles to Monitor.Enter and Monitor.Exit. Learn when the higher-level syntax is enough and when you need Monitor's advanced features like timeouts and thread signaling.

To choose between C# Monitor and the lock statement, focus on the behavior you need: lock is the safer default for ordinary mutual exclusion, while Monitor is for cases where you need timeout handling, non-blocking attempts, or thread signaling.
What the lock Statement Actually Compiles To
For a typical object lock, the lock statement is not a separate synchronization primitive. When you write:
private readonly object _sync = new object();
public void Update()
{
lock (_sync)
{
// critical section
}
}the compiler translates it into a Monitor.Enter call followed by a Monitor.Exit call in a finally block. Since C# 4, the generated code uses the lockTaken overload so that Exit is only called when the lock was actually acquired:
public void Update()
{
object sync = _sync;
bool lockTaken = false;
try
{
Monitor.Enter(sync, ref lockTaken);
// critical section
}
finally
{
if (lockTaken)
{
Monitor.Exit(sync);
}
}
}The lockTaken parameter matters because Monitor.Enter can throw before the lock is acquired, for example if the waiting thread is interrupted. Without the flag, a finally block that unconditionally calls Monitor.Exit would throw a SynchronizationLockException, because the current thread does not own the monitor.
So the practical answer is that lock is Monitor with a safer default shape. The runtime behavior is the same when you use the equivalent try/finally shape manually: both use the monitor entry and exit mechanism.
When Manual Monitor.Enter and Monitor.Exit Are Necessary
The lock statement always blocks until the monitor is acquired. There is no built-in way to express a timeout or a non-blocking attempt with lock. That is where Monitor's additional API surface matters.
Monitor.TryEnter accepts a timeout and returns false when the lock could not be acquired within the given time:
if (Monitor.TryEnter(_sync, TimeSpan.FromSeconds(2)))
{
try
{
// critical section
}
finally
{
Monitor.Exit(_sync);
}
}
else
{
// The lock was not acquired within 2 seconds.
// Handle the contention case explicitly.
}This pattern is useful in operations that must not stall indefinitely, such as a background worker that should give up and retry later, or a UI-triggered operation that should surface a timeout to the user instead of freezing the thread.
Monitor.TryEnter also has an overload that takes an integer milliseconds value, and both time-based overloads have variants with a ref bool lockTaken parameter. If you write a try/finally block around the acquisition call, use the lockTaken variant so Monitor.Exit is called only when the lock was actually acquired.
Signaling Between Threads with Monitor.Wait and Monitor.Pulse
The lock statement gives you mutual exclusion, but it gives you no way to coordinate threads beyond that. Monitor.Wait, Monitor.Pulse, and Monitor.PulseAll provide a basic signaling mechanism that lock cannot express.
Monitor.Wait releases the monitor and blocks the calling thread until it is pulsed. Monitor.Pulse wakes one waiting thread; Monitor.PulseAll wakes all of them. Both must be called from inside the monitor.
A typical producer-consumer handoff looks like this:
private readonly object _sync = new object();
private bool _ready;
public void Produce()
{
lock (_sync)
{
_ready = true;
Monitor.Pulse(_sync);
}
}
public void Consume()
{
lock (_sync)
{
while (!_ready)
{
Monitor.Wait(_sync);
}
// _ready is true here.
}
}The while loop matters. Monitor.Wait can return even when the condition is still false, because another thread may have pulsed and then changed the state before the waiting thread reacquired the monitor. Rechecking the condition in a loop is the standard way to handle that.
Reentrancy and Nested Locking
Both lock and Monitor are reentrant for monitor-based locks. The same thread can enter the same monitor multiple times, and each entry must be balanced by a corresponding exit. The lock statement handles that balance automatically because each lock block emits its own Monitor.Exit in its finally block.
Manual Monitor.Enter/Monitor.Exit calls are where reentrancy becomes a maintenance risk. If a method acquires the monitor and then calls another method that also acquires it, you now have two Enter calls and need two Exit calls:
public void Outer()
{
Monitor.Enter(_sync);
try
{
Inner();
}
finally
{
Monitor.Exit(_sync);
}
}
public void Inner()
{
Monitor.Enter(_sync);
DoWork(); // if this throws, the monitor stays held
Monitor.Exit(_sync);
}If DoWork throws, Inner never reaches its Monitor.Exit, and the monitor remains held by the current thread. Every other thread that tries to enter the monitor blocks indefinitely. The lock statement prevents this class of bug because the compiler emits the Exit in a finally block.
Runtime Cost and Contention Behavior
The lock statement does not add meaningful overhead compared to calling Monitor.Enter and Monitor.Exit directly, because it compiles to those exact calls. The performance difference between lock and Monitor is therefore not about the locking mechanism itself but about which Monitor methods you use.
Monitor.Enter blocks the thread when the monitor is held by another thread. Blocking involves a kernel transition, which is significantly more expensive than the uncontended fast path where the lock is acquired with an atomic compare-and-exchange. Monitor.TryEnter with a timeout behaves the same way when it blocks, but it gives you the option to avoid blocking entirely by passing a zero timeout.
The cost of Monitor.Wait and Monitor.Pulse is higher than plain enter/exit because they involve releasing the monitor, blocking the thread, and later reacquiring the monitor when pulsed. If you only need mutual exclusion, using Wait/Pulse adds complexity and runtime cost without benefit.
One operational detail worth knowing: Monitor is not safe to use across await boundaries. The monitor is held by a thread, and an await can resume on a different thread, so the Exit call can throw a SynchronizationLockException. The compiler prevents await inside a lock block, but manual Monitor.Enter/Exit across an await is fragile and can throw at runtime.
Choosing Between lock and Monitor
Use lock as the default for any critical section that only needs mutual exclusion. It is shorter, it generates the correct try/finally shape, and it eliminates the class of bugs where Monitor.Exit is skipped on an exception path.
Reach for Monitor directly when you need one of these specific behaviors:
Monitor.TryEnterwith a timeout or a non-blocking attemptMonitor.WaitandMonitor.Pulsefor thread signalingMonitor.Enterwith thelockTakenoverload in code that must handle the case where the lock acquisition itself throws
If none of those apply, lock is the safer and more readable choice. It is the correct default shape for ordinary mutual exclusion, and Monitor's extra methods exist for the cases where that default is not enough.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.