Anti-Pattern
An pattern stands for best practices, an anti-pattern stands for a lesson learned, or to use the words from Andrew Koenig: “Those that describe a bad solution to a problem which resulted in a bad situation.” If you carefully read the literature to concurrency patterns, you often find the double-checked locking pattern. The general idea of the double-checked locking pattern is in one sentence the thread-safe initialisation of shared state in an optimised way. This shared state is typically a singleton. I intentionally put the double-checked locking pattern in the case studies chapter of this book to emphasise it explicitly: naively usage of the double-checked locking pattern may end in undefined behaviour. The issues of the double-checked locking pattern boil essentially down to the issues of the singleton pattern.
If you want to use the singleton pattern, you have to think about the following challenges:
- First and foremost, the singleton is a global...