Buradasın
Double-checked Locking Design Pattern
en.wikipedia.org/wiki/Double-checked_lockingYapay zekadan makale özeti
- Problem and Motivation
- Double-checked locking reduces lock acquisition overhead by testing locking criterion before acquiring lock
- Pattern was introduced in Pattern Languages of Program Design 3
- Original form has data races and is considered anti-pattern
- Implementation Challenges
- Memory model affects correct implementation of algorithm
- Naive implementation appears to work most of the time
- Incorrect implementation can cause program crashes
- Valid Implementations
- Java uses volatile keyword for memory barrier
- C++11 provides std::once_flag and std::call_once
- .NET Framework 4.0 uses Lazy<T> class with double-checked locking
- Alternatives
- Initialization-on-demand holder idiom for static objects
- Final field semantics in Java 5
- Java 9 introduced VarHandle class for relaxed atomics
- Singleton Pattern
- Double-checked locking not needed for singleton pattern
- Concurrency should wait for initialization completion
- Initialization-on-demand holder idiom available in Java