Repository navigation
Concurrency and Database Lifetime
Do not add volatile, Volatile.Read, or Volatile.Write merely because multiple threads read a field. First establish when it can change and how its initialization and lifetime are synchronized.
Blueprint uses one database instance per application. Initialization must complete before consumer threads start, and disposal must run only after all consumers, including queued operations and cache callbacks, have stopped. Thread startup and shutdown must provide the synchronization that publishes initialization and waits for consumers to finish. Under this lifecycle, fActiveInstance is read directly and is not declared volatile:
private static BlueprintDatabase? fActiveInstance;Keep Interlocked operations where atomic updates are required, such as publishing the active database only when the field is null or clearing it only when it still refers to the disposing instance. Do not replace compare-and-exchange with a separate check and assignment.
If another field can change while other threads access it, choose synchronization for that actual access pattern and document the reason. When volatile read/write semantics are needed and C# supports the modifier for the field's type, prefer a volatile declaration with direct access. Use explicit Volatile operations when the declaration cannot carry the modifier or when combining a plain field with Interlocked operations avoids passing a volatile field by reference. Do not add warning suppressions merely to enforce a volatile declaration.
volatile does not make compound operations atomic, protect the referenced object's mutable state, or prevent disposal after a reference is read. Retain the locks and lifetime checks required by shared contexts, transactions, caches, and disposal. Fields whose accesses are already consistently protected by a lock do not need volatile solely because multiple threads use them.