Is the C static constructor thread safe?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In C#, static constructors are a specialized type of constructor used to initialize a class itself, rather than an instance of it. One of the often-discussed aspects of static constructors is their thread safety, which is a critical component when working with multithreaded applications. This article will explore the thread safety mechanisms provided by static constructors in C#.
Understanding Static Constructors in C#
A static constructor is a special method that initializes static fields of a class. It runs automatically and is called only once, before any static members of the class are accessed or any instances are created. Here's a simple example:
In the example above, the static constructor is defined using the static keyword and does not have any parameters. It's primarily used to initialize static fields or to enforce some invariant behavior across all instances of a class.
Thread Safety of Static Constructors
C# guarantees that static constructors are thread-safe. Here are some key points about the thread-safety of static constructors:
- Single Invocation: The static constructor for a class is guaranteed to be invoked only once during the lifetime of the application domain. This ensures that the initialization logic is not executed multiple times concurrently, which could lead to inconsistent state or race conditions.
- Automatic Synchronization: The runtime automatically locks the type during the execution of a static constructor. This means that even if multiple threads try to access the class at the same time, the static constructor is called serially.
- Before Access: A static constructor will be run before any static members are accessed for the first time or any instance of the class is created. This lazy initialization ensures that the class members are in a consistent state before being used.
Example of Thread Safety in Practice
Consider the following example where multiple threads might attempt to access a static member of a class:
Here, no matter how many threads call Logger.Log, the message from the static constructor will be printed exactly once to the console, ensuring that the class initialization is safely handled.
Potential Pitfalls
While static constructors are thread-safe by design, there are some potential pitfalls developers should be aware of:
- Deadlocks: If a static constructor tries to acquire a lock which is already held by another thread that is waiting for the static constructor to complete, it can lead to a deadlock.
- Exception Handling: If a static constructor throws an exception, the CLR will not attempt to invoke it again, and the type will remain uninitialized. Further attempts to use the class will trigger a
TypeInitializationException.
Summarizing Synchronization Features
Below is a table summarizing key points about the thread safety of static constructors:
| Feature | Description |
| Single Invocation | Called only once per application domain. |
| Automatic Synchronization | Locked by runtime to ensure serial execution. |
| Execution Timing | Runs before any static member access or instance creation. |
| Exception Handling | Exceptions prevent re-execution. |
| Risk of Deadlocks | Must avoid locks held by other threads waiting on class init. |
Conclusion
Static constructors in C# offer robust thread safety features, ensuring that class-level initialization logic is executed in a controlled and consistent manner. This automatic handling simplifies the development of multithreaded applications, allowing developers to focus more on business logic without worrying about intricate synchronization issues during class initialization. However, careful attention is needed to avoid deadlocks and manage exceptions properly.
Related reading
- Is the check thread safe?
- Is the class generator inheriting Sequence thread safe in Keras/Tensorflow?
- Is the lock statement reentrant in C?
- Is the lock statement reentrant in C?
- Is the sorting algorithm used by .NET's Array.Sort method a stable algorithm?
- Is the VC code DOM accessible from VS addons?
- Is the popular volatile polled flag pattern broken?
- Is there a better waiting pattern for c?

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.