Interlocked and volatile
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Interlocked and volatile both appear in multithreaded C# code, but they solve different problems. volatile is about visibility and ordering of reads and writes, while Interlocked is about atomic operations such as incrementing, exchanging, or compare-and-swap on shared values.
What volatile Does
Marking a field as volatile tells the runtime and compiler not to treat reads and writes to that field like an ordinary cached variable access. The goal is to ensure that one thread sees another thread's updates in a timely and correctly ordered way.
Here volatile is appropriate because the field is a simple shared flag.
What volatile Does Not Do
volatile does not make compound operations atomic. This is the most important limitation.
The increment still means:
- read the current value
- add one
- write the new value
Another thread can interleave with those steps, so volatile alone does not protect counters from race conditions.
What Interlocked Does
Interlocked provides atomic operations on shared values. These operations happen as one indivisible unit from the perspective of competing threads.
This safely increments the shared counter without using a full lock.
Compare-And-Swap With Interlocked.CompareExchange
One of the most powerful Interlocked operations is compare-and-swap.
This sets _initialized to 1 only if it was previously 0, and does so atomically.
When to Use Each One
A simple rule works well:
- use
volatilefor simple state flags or references where visibility is the main concern - use
Interlockedfor atomic updates to shared numeric values or references - use
lockwhen several operations must be protected together as one critical section
That distinction prevents many incorrect uses of volatile.
lock Is Still Important
Neither volatile nor Interlocked replaces lock for larger invariants.
If several fields must be updated together consistently, or if a read depends on several related values matching each other, a lock is often the right tool.
Interlocked is excellent for small atomic operations. It is not a complete replacement for higher-level synchronization.
Common Pitfalls
The biggest pitfall is using volatile for counters and assuming it prevents lost updates. It does not, because incrementing is not an atomic operation.
Another issue is overusing Interlocked when the code really needs a proper critical section around several related operations.
Developers also treat volatile as a general thread-safety keyword. It is not. It gives visibility guarantees for that field, not full synchronization for the surrounding object state.
Finally, do not mix several concurrency techniques casually without a clear reason. Concurrency bugs are hard enough when the design is consistent.
Summary
- '
volatileis about visibility and ordering of field access.' - '
Interlockedis about atomic operations on shared values.' - '
volatiledoes not makex++safe.' - Use
Interlockedfor counters, swaps, and compare-and-set patterns. - Use
lockwhen a whole sequence of operations must stay consistent together.
Related reading
- Interrupt a sleeping Thread
- Interview Questions on Socket Programming and Multi-Threading
- Invalid token 'void' when compling async..await on build server
- invoke aws lambda from another lambda asynchronously
- internal vs public in C
- InternalsVisibleTo attribute isn't working
- Invoke or BeginInvoke cannot be called on a control until the window handle has been created
- IO Completion Ports IOCP

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.