-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-thread-safe-singleton.py
More file actions
447 lines (329 loc) · 13.4 KB
/
03-thread-safe-singleton.py
File metadata and controls
447 lines (329 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
"""Question: Implement a class ThreadSafeSingleton using threading to ensure
thread-safe singleton behavior.
"""
# LEARNING CHALLENGE
#
# Before looking at any solution below, please try to solve this yourself first!
#
# Tips for success:
# - Read the question carefully
# - Think about what classes and methods you need
# - Start with a simple implementation
# - Test your code step by step
# - Don't worry if it's not perfect - learning is a process!
#
# Remember: The best way to learn programming is by doing, not by reading solutions!
#
# Take your time, experiment, and enjoy the learning process!
# Try to implement your solution here:
# (Write your code below this line)
# HINT SECTION (Only look if you're really stuck!)
#
# Think about:
# - What are race conditions? (multiple threads accessing shared resources)
# - What is threading.Lock()? (mechanism to prevent concurrent access)
# - Where should you use the lock? (around the instance creation check)
# - Why is thread safety important for singletons?
#
# Remember: Start simple and build up complexity gradually!
# ===============================================================================
# STEP-BY-STEP SOLUTION
# ===============================================================================
#
# CLASSROOM-STYLE WALKTHROUGH
#
# Let's solve this problem step by step, just like in a programming class!
# Each step builds upon the previous one, so you can follow along and understand
# the complete thought process.
#
# ===============================================================================
# Step 1: Import threading and understand the problem
# ===============================================================================
# Explanation:
# In multi-threaded environments, regular singleton implementations can fail.
# Multiple threads might simultaneously check if an instance exists and create multiple instances.
import threading
import time
print("Step 1: Understanding thread safety issues")
# What we accomplished in this step:
# - Imported necessary threading module
# - Identified the thread safety problem
# Step 2: Create basic ThreadSafeSingleton class structure
# ===============================================================================
# Explanation:
# Let's start with the basic structure including a class attribute for the instance
# and a threading lock to control access.
import threading
import time
class ThreadSafeSingleton:
_instance = None
_lock = threading.Lock()
print("Step 2: Basic ThreadSafeSingleton structure created")
# What we accomplished in this step:
# - Created class with instance storage and lock
# Step 3: Implement thread-safe __new__ method
# ===============================================================================
# Explanation:
# The __new__ method controls object creation. We use a lock to ensure only one thread
# can check and create the instance at a time.
import threading
import time
class ThreadSafeSingleton:
_instance = None
_lock = threading.Lock()
def __new__(cls):
# Double-checked locking pattern
if cls._instance is None:
with cls._lock:
# Check again inside the lock
if cls._instance is None:
print(f"Creating new instance in thread {threading.current_thread().name}")
cls._instance = super(ThreadSafeSingleton, cls).__new__(cls)
return cls._instance
print("Step 3: Thread-safe __new__ method implemented")
# What we accomplished in this step:
# - Implemented double-checked locking pattern
# - Added thread identification for testing
# Step 4: Add initialization with thread safety
# ===============================================================================
# Explanation:
# We need to ensure that __init__ is only called once, even in multi-threaded environments.
# We'll use a flag to track initialization.
import threading
import time
class ThreadSafeSingleton:
_instance = None
_lock = threading.Lock()
_initialized = False
def __new__(cls):
if cls._instance is None:
with cls._lock:
if cls._instance is None:
print(f"Creating new instance in thread {threading.current_thread().name}")
cls._instance = super(ThreadSafeSingleton, cls).__new__(cls)
return cls._instance
def __init__(self):
if not ThreadSafeSingleton._initialized:
with ThreadSafeSingleton._lock:
if not ThreadSafeSingleton._initialized:
print(f"Initializing instance in thread {threading.current_thread().name}")
self.value = None
self.created_at = time.time()
ThreadSafeSingleton._initialized = True
print("Step 4: Thread-safe initialization added")
# What we accomplished in this step:
# - Added thread-safe initialization
# - Prevented multiple initialization calls
# Step 5: Add useful methods and properties
# ===============================================================================
# Explanation:
# Let's add some methods to make our singleton more useful and demonstrate
# that state is shared across all "instances".
import threading
import time
class ThreadSafeSingleton:
_instance = None
_lock = threading.Lock()
_initialized = False
def __new__(cls):
if cls._instance is None:
with cls._lock:
if cls._instance is None:
print(f"Creating new instance in thread {threading.current_thread().name}")
cls._instance = super(ThreadSafeSingleton, cls).__new__(cls)
return cls._instance
def __init__(self):
if not ThreadSafeSingleton._initialized:
with ThreadSafeSingleton._lock:
if not ThreadSafeSingleton._initialized:
print(f"Initializing instance in thread {threading.current_thread().name}")
self.value = None
self.created_at = time.time()
self.access_count = 0
ThreadSafeSingleton._initialized = True
def set_value(self, value):
with ThreadSafeSingleton._lock:
self.value = value
def get_value(self):
with ThreadSafeSingleton._lock:
self.access_count += 1
return self.value
def get_info(self):
with ThreadSafeSingleton._lock:
return {
'id': id(self),
'value': self.value,
'created_at': self.created_at,
'access_count': self.access_count,
'thread': threading.current_thread().name
}
print("Step 5: Added useful methods with thread safety")
# What we accomplished in this step:
# - Added thread-safe methods for value manipulation
# - Added access counting and information retrieval
# Step 6: Create test functions for multi-threading
# ===============================================================================
# Explanation:
# Let's create functions to test our thread-safe singleton in a multi-threaded environment.
import threading
import time
import random
class ThreadSafeSingleton:
_instance = None
_lock = threading.Lock()
_initialized = False
def __new__(cls):
if cls._instance is None:
with cls._lock:
if cls._instance is None:
print(f"Creating new instance in thread {threading.current_thread().name}")
cls._instance = super(ThreadSafeSingleton, cls).__new__(cls)
return cls._instance
def __init__(self):
if not ThreadSafeSingleton._initialized:
with ThreadSafeSingleton._lock:
if not ThreadSafeSingleton._initialized:
print(f"Initializing instance in thread {threading.current_thread().name}")
self.value = None
self.created_at = time.time()
self.access_count = 0
ThreadSafeSingleton._initialized = True
def set_value(self, value):
with ThreadSafeSingleton._lock:
self.value = value
def get_value(self):
with ThreadSafeSingleton._lock:
self.access_count += 1
return self.value
def get_info(self):
with ThreadSafeSingleton._lock:
return {
'id': id(self),
'value': self.value,
'created_at': self.created_at,
'access_count': self.access_count,
'thread': threading.current_thread().name
}
def create_and_test_singleton(thread_id):
"""Function to be run in multiple threads"""
print(f"Thread {thread_id} starting...")
# Simulate some work before creating singleton
time.sleep(random.uniform(0.01, 0.1))
# Create singleton instance
singleton = ThreadSafeSingleton()
# Test setting and getting values
singleton.set_value(f"Value from thread {thread_id}")
time.sleep(0.01) # Small delay
value = singleton.get_value()
info = singleton.get_info()
print(f"Thread {thread_id} - Instance ID: {info['id']}, Value: {value}")
return singleton
print("Step 6: Test functions created")
# What we accomplished in this step:
# - Created test function for multi-threaded testing
# - Added random delays to simulate real-world conditions
# Step 7: Test the thread-safe singleton
# ===============================================================================
# Explanation:
# Finally, let's test our thread-safe singleton with multiple threads to verify
# that only one instance is created and shared properly.
import threading
import time
import random
class ThreadSafeSingleton:
_instance = None
_lock = threading.Lock()
_initialized = False
def __new__(cls):
if cls._instance is None:
with cls._lock:
if cls._instance is None:
print(f"Creating new instance in thread {threading.current_thread().name}")
cls._instance = super(ThreadSafeSingleton, cls).__new__(cls)
return cls._instance
def __init__(self):
if not ThreadSafeSingleton._initialized:
with ThreadSafeSingleton._lock:
if not ThreadSafeSingleton._initialized:
print(f"Initializing instance in thread {threading.current_thread().name}")
self.value = None
self.created_at = time.time()
self.access_count = 0
ThreadSafeSingleton._initialized = True
def set_value(self, value):
with ThreadSafeSingleton._lock:
self.value = value
def get_value(self):
with ThreadSafeSingleton._lock:
self.access_count += 1
return self.value
def get_info(self):
with ThreadSafeSingleton._lock:
return {
'id': id(self),
'value': self.value,
'created_at': self.created_at,
'access_count': self.access_count,
'thread': threading.current_thread().name
}
def create_and_test_singleton(thread_id):
print(f"Thread {thread_id} starting...")
time.sleep(random.uniform(0.01, 0.1))
singleton = ThreadSafeSingleton()
singleton.set_value(f"Value from thread {thread_id}")
time.sleep(0.01)
value = singleton.get_value()
info = singleton.get_info()
print(f"Thread {thread_id} - Instance ID: {info['id']}, Value: {value}")
return singleton
# Test our thread-safe singleton:
print("Testing ThreadSafeSingleton with multiple threads:")
# Create and start multiple threads
threads = []
results = []
def thread_worker(thread_id, results_list):
result = create_and_test_singleton(thread_id)
results_list.append(result)
# Start 5 threads simultaneously
for i in range(5):
thread = threading.Thread(target=thread_worker, args=(i, results))
threads.append(thread)
thread.start()
# Wait for all threads to complete
for thread in threads:
thread.join()
# Verify all instances are the same
print(f"\nVerification:")
print(f"Number of results: {len(results)}")
first_instance_id = id(results[0])
all_same = all(id(instance) == first_instance_id for instance in results)
print(f"All instances are the same object: {all_same}")
# Get final state
final_singleton = ThreadSafeSingleton()
final_info = final_singleton.get_info()
print(f"Final singleton info: {final_info}")
# What we accomplished in this step:
# - Created and tested our complete thread-safe singleton
# - Verified that only one instance exists across multiple threads
# - Demonstrated shared state in multi-threaded environment
# ===============================================================================
# CONGRATULATIONS!
#
# You've successfully completed the step-by-step solution!
#
# Key concepts learned:
# - Thread safety and race conditions
# - Double-checked locking pattern
# - Threading locks and synchronization
# - Multi-threaded singleton implementation
# - Thread-safe method design
# - Testing concurrent code
#
# Try it yourself:
# 1. Start with Step 1 and code along
# 2. Test each step before moving to the next
# 3. Understand WHY each step is necessary
# 4. Experiment with modifications (try adding performance monitoring or different locking strategies!)
#
# Remember: The best way to learn is by doing!
# ===============================================================================