You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since buffer_not_full and lock share the same lock, wouldn't with buffer_not_full and with lock within with buffer_not_full contextmanager lead to a deadlock issue?
fromthreadingimportThread, Lock, ConditionimporttimefromqueueimportQueue# initialize buffer, shared by producer and consumerbuffer=Queue(maxsize=10)
# lock for controlled buffer accesslock=Lock()
# condition to signal when the buffer is not full/emptybuffer_not_full=Condition(lock)
buffer_not_empty=Condition(lock)
classProducer(Thread):
defrun(self):
foriinrange(100):
withbuffer_not_full: # 1. acquires the lockwhilebuffer.full():
buffer_not_full.wait()
withlock: # 2. lock has been acquired by `buffer_not_full`, but attempts to obtain the same lock?buffer.put(i)
print(f"Produced: {i}")
buffer_not_empty.notify()
classConsumer(Thread):
defrun(self):
foriinrange(100):
withbuffer_not_empty:
whilebuffer.empty():
buffer_not_empty.wait()
withlock:
item=buffer.get()
print(f"Consumed: {item}")
buffer_not_full.notify()
# start producer and consumer threadsproducer=Producer()
consumer=Consumer()
producer.start()
consumer.start()
The text was updated successfully, but these errors were encountered:
Since
buffer_not_full
andlock
share the same lock, wouldn'twith buffer_not_full
andwith lock
withinwith buffer_not_full
contextmanager lead to a deadlock issue?The text was updated successfully, but these errors were encountered: