Is a bool read/write atomic in C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In C#, the concept of atomic operations or atomicity is crucial for building robust multi-threaded applications. Atomicity refers to operations that are completed as a single, indivisible unit. If an operation is atomic, once it's started, no other operations can see it in an incomplete state, which ensures consistent and predictable behavior in concurrent programming. One may often wonder whether the simple reading and writing of a bool type variable in C# is atomic. This article delves into the technicalities of bool's atomicity in C# and includes examples and data that illustrate this topic.
Understanding Atomic Operations in C#
Atomic operations prevent interruptions midway, thus eliminating issues such as race conditions. In the context of C#, atomicity is mostly concerned with hardware and memory models that enable safe concurrent access to shared data without locking mechanisms. This ensures that such operations are swift and efficient, especially important in performance-critical applications.
Are bool Reads/Writes Atomic in C#?
In C#, the bool type is typically represented as a 4-byte integer under the hood, contrary to expectations that might peg it as a 1-byte storage type. The CLR (Common Language Runtime) of .NET provides certain guarantees for types up to 4 bytes in size on a 32-bit architecture. Therefore, in most implementations, reading and writing a bool value is atomic. However, there are some potential caveats and considerations, particularly in the context of different architectures.
Key Points:
- Atomicity on 32-bit Systems: On 32-bit systems, reading from and writing to a
boolis atomic because these systems can naturally handle 4-byte data types atomically. - Atomicity on 64-bit Systems: On 64-bit systems, the same logic follows, but here it's because
boolis smaller than a single 8-byte alignment unit. Thus,boolis treated as a naturally aligned type on both architectures, which maintains atomic operations. - Non-Atomic Scenarios: Although reads and writes are typically atomic, modifying a
boolwith certain constructs likeInterlockedis not applicable directly sinceInterlockedoperations work onint,long, or other numeric types. - Volatile Keyword: Using the
volatilekeyword can ensure that the compiler and the processor do not cache the variable, optimizing concurrent operations, though it doesn’t impact atomicity but visibility.
An Example:
Related reading
- Is a Go goroutine a coroutine?
- Is a HashMap thread-safe for different keys?
- Is armadillo solve thread safe?
- Is async await truly non-blocking in the browser?
- Is a memory leak created if a MemoryStream in .NET is not closed?
- Is DateTime.Now the best way to measure a function's performance?
- is async/await slower than promises?
- Is asynchronous jdbc call possible?

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.