59. Thread Synchronization with Locks
Here are some Python code snippets that demonstrate how to use thread locks for synchronization in multi-threading:
1. Basic Thread Synchronization with Lock
This example uses a lock to ensure that only one thread can access a critical section at a time.
import threading
# Shared resource
counter = 0
lock = threading.Lock()
def increment():
global counter
with lock: # Acquire the lock
temp = counter
temp += 1
counter = temp
threads = []
for _ in range(100):
thread = threading.Thread(target=increment)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
print(f"Counter value: {counter}") # Output: 100In this code, the increment function updates the global counter variable. The lock is used to ensure that only one thread can modify counter at a time.
2. Using Lock with a Shared Resource
In this example, we have multiple threads that increment and decrement a shared resource safely using a lock.
The lock ensures that both increment and decrement functions safely modify the shared resource without any race conditions.
3. Reentrant Lock (RLock) Example
A reentrant lock allows a thread to lock the same lock multiple times. Here's how to use an RLock for nested locking scenarios.
In this example, the RLock allows the same thread to lock the resource multiple times without causing a deadlock.
4. Deadlock Avoidance Using Locks
Deadlock can occur when two or more threads wait for each other to release a resource. This example shows how to avoid deadlock using two locks.
By acquiring the locks in the same order (lock1 first, then lock2), you avoid deadlock.
5. Locking with Condition Variables
A Condition is used to synchronize threads by having them wait for a particular condition to be met. This example shows how to use a condition with a lock.
In this example, two threads wait for a condition to be met, and one thread notifies them after a delay.
6. Using Lock for Thread-safe Queue
A lock can be used to ensure that threads can safely interact with a shared queue.
In this example, a lock is used to ensure that the producer and consumer threads do not access the queue simultaneously.
7. Using Lock to Protect File Writing
A lock can be used to protect file writing in a multi-threaded environment.
In this example, multiple threads are safely writing to the same file using a lock.
8. Locking with Thread Pool Executor
The ThreadPoolExecutor can be combined with locks for efficient thread management in tasks requiring synchronization.
This example uses a thread pool to manage multiple threads and ensures synchronization via a lock.
9. Locking with Timer Thread
This example shows how a timer thread can be synchronized using a lock.
The lock ensures that the timer thread's output is thread-safe and prevents race conditions with other threads.
10. Read-Write Lock Simulation
This example simulates a read-write lock using a lock to control multiple readers and a single writer.
In this example, multiple reader threads can run concurrently, but the writer thread is given exclusive access.
These examples demonstrate various ways to use locks in Python's threading module to ensure thread synchronization and prevent race conditions.
Last updated