Tackling Thread Safety in Python
summary
A simple booking example runs 15 concurrent tasks against 10 seats and ends with available_seats at -5 when using ThreadPoolExecutor because of race conditions; wrapping the critical section with a mutex via lock.acquire/release fixes overselling. A recursive nested-acquire deadlock becomes safe with an RLock. A Semaphore initialized to 3 limits concurrent bookings to three, an Event signals “flight landed” to waiting threads, a Barrier waits for five participants before proceeding, and a Condition implements a producer–consumer customer queue.
AI-written summary · about
chapters (15)
- 0:00Introduction to thread safety
- 2:21Speaker introduction and agenda
- 3:04Why use threading in Python
- 6:40Race conditions in shared data
- 7:23Context switching demonstration
- 10:18What is thread safety
- 11:46Options for thread-safe code
- 13:11Using locks for thread safety
- 16:24Reentrant locks explained
- 19:43Semaphores for limiting access
- 22:54Events for thread signaling
- 25:48Barriers for thread coordination
- 30:04Conditions for producer-consumer
- 38:37Key considerations for thread safety
- 40:23Q&A session
