Skip to content

Commit 60d61f1

Browse files
authored
Revise README for Singleton design pattern examples
Updated README to enhance clarity and modernize language for Singleton examples.
1 parent 79f4ea9 commit 60d61f1

1 file changed

Lines changed: 54 additions & 17 deletions

File tree

README.md

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,61 @@ A collection of minimal, self-contained C++ examples demonstrating multiple ways
44

55
## 🔍 Overview
66

7-
### singleton-meyers-example
8-
* Meyer’s Singleton - Example with STATIC LOCAL VARIABLE
9-
* Static memory allocation
10-
* Lazy initialization
11-
* Singleton is created only after first call of getInstance() and destroyed after main() call
12-
* Thread-safe - Thread safety is guaranteed since C++11.
7+
### ⭐ singleton-meyers-example
8+
* Meyers Singleton — the simplest and safest modern C++ singleton implementation.
9+
* 🧩 Static local variable
10+
* 💾 Static memory allocation
11+
* ⏳ Lazy initialization
12+
* 🧼 Automatic cleanup (destroyed after main() exits)
13+
* 🔒 Thread-safe since C++11
1314
* A function-local static variable is initialized exactly once, even in a multi-threaded environment.
14-
* This is the best and simplest way to implement a singleton in C++11 and later.
15-
16-
### singleton-cherno-example
17-
18-
### singleton-classic-static-example
19-
20-
### singleton-classic-dynamic-example
21-
22-
### singleton-dclp-example
23-
24-
### singleton-smart-pointer-example
15+
* 🟢 This is the best and simplest way to implement a singleton in C++11 and later.
16+
17+
### ⭐ singleton-cherno-example
18+
* Cherno-style Singleton - https://youtu.be/IMZMLvIwa-k?si=Q__9r--DOre6jahY
19+
* 🧩 Static global variable
20+
* 💾 Dynamic memory allocation
21+
* ⏳ Lazy initialization
22+
* 🧹 Manual destruction required (via delInstance())
23+
* ⚠️ Not thread-safe
24+
* 🔴 Suitable only for single-threaded
25+
26+
### ⭐ singleton-classic-static-example
27+
* Singleton with a static member instance — created eagerly at program startup.
28+
* 🧩 Static member variable
29+
* 💾 Static memory allocation
30+
* ⚡ Eager initialization (constructed before main() starts)
31+
* 🧼 Automatic destruction after main() exits
32+
* ⚠️ Not thread-safe
33+
* 🔴 Can suffer from the static initialization order fiasco
34+
35+
### ⭐ singleton-classic-dynamic-example
36+
* Singleton with a static member pointer — dynamically allocated on first use.
37+
* 🧩 Static member pointer
38+
* 💾 Dynamic memory allocation
39+
* ⏳ Lazy initialization (created only on first call to getInstance())
40+
* 🧹 Requires manual destruction via delInstance()
41+
* ⚠️ Not thread-safe
42+
* 🔴 Not recommended for multi-threaded applications
43+
44+
### ⭐ singleton-dclp-example
45+
* Double-Checked Locking Pattern (DCLP) — classic but unsafe lazy-initialization pattern.
46+
* 🧩 Static member pointer
47+
* 💾 Dynamic memory allocation
48+
* ⏳ Lazy initialization
49+
* 🧹 Destroyed manually via delInstance()
50+
* ⚠️ Not thread-safe in C++ — suffers from data races and reordering issues
51+
* ❌ DCLP is unreliable because multiple threads may observe a partially constructed object
52+
* ⛔ Obsoleted by C++11 (local static initialization is the correct modern solution)
53+
* Reference: https://www.aristeia.com/Papers/DDJ_Jul_Aug_2004
54+
55+
### ⭐ singleton-smart-pointer-example
56+
* Singleton using a static local smart pointer
57+
* 🧩 Static local variable
58+
* 💾 Dynamic memory allocation
59+
* ⏳ Lazy initialization (instance created on first getInstance() call)
60+
* 🧼 Automatically destroyed after main() exits
61+
* 🔒 Thread-safe initialization (C++11+)
2562

2663
## ⚙️ Prerequisites
2764

0 commit comments

Comments
 (0)