Complete the code to acquire a lock before accessing shared data.
lock.[1]() # Acquire the lock before critical section
To safely access shared data, you must acquire the lock first using acquire().
Complete the code to safely release the lock after the critical section.
try: # critical section code pass finally: lock.[1]() # Release the lock
After finishing the critical section, always release the lock with release() to allow others to proceed.
Fix the error in the code to avoid race conditions when updating a shared counter.
def increment(): global counter [1] # Missing synchronization here counter += 1 lock.release()
Before modifying shared data, acquire the lock to prevent race conditions.
Fill both blanks to implement a thread-safe queue using locks.
def enqueue(item): [1] # Acquire lock queue.append(item) [2] # Release lock
Acquire the lock before modifying the queue and release it after to ensure thread safety.
Fill all three blanks to implement a safe read-modify-write operation with locks.
def update_value(key, delta): [1] # Acquire lock current = data.get([2], 0) data[[3]] = current + delta lock.release()
Acquire the lock first, then use the key to get and update the value safely.
