This project is a simple Python script that demonstrates advanced, low-level threading synchronization using threading.Condition.
It solves the classic "Producer-Consumer" problem:
- 5 Consumer Threads (
waiter) start first. They want to get a password from a shared list, but the list is empty. They go to sleep andwait()for a signal. - 1 Producer Thread (the main thread) then asks the user for a password (it "produces" data).
- After getting a password, it adds it to the shared list and sends a
notify()signal. - This signal wakes up one of the sleeping threads, which can now safely "consume" the password from the list before going back to sleep.
This script is a technical demonstration of advanced computer science concepts:
- Multithreading: Running multiple threads of execution concurrently.
- Synchronization: Using a
threading.Conditionobject to prevent a "race condition" (where multiple threads might try to access the empty list at the same time). - Signaling: Using
cond.wait()to pause threads andcond.notify()to wake them up efficiently, ensuring the program doesn't waste CPU cycles. - Resource Locking: The
cond.acquire()andcond.release()(which are part of theConditionobject) ensure that only one thread can modify the shared list at any given moment.
- Download the
main.pyfile. - Run it using Python 3:
python3 main.pyThe program will immediately start 5 "waiter" threads (which will go to sleep) and then prompt you for a password. Each time you enter a password and press Enter, one of the waiting threads will wake up and print it.