【发布时间】:2018-12-11 03:53:04
【问题描述】:
这是最大大小的缓冲区的问题。如果缓冲区已满,生产者无法生产,如果缓冲区为空,消费者需要等待。显然,这种使用信号量的解决方案仅适用于单个生产者和消费者。谁能解释一下为什么它不适用于多个生产者和消费者?
has_space = Semaphore(max_num)
has_elements = Semaphore(0)
mutex = lock()
def producer():
has_space.acquire()
with mutex:
# add elements
has_elements.release()
def consumer():
has_elements.acquire()
with mutex:
# retrieve elements
has_space.release()
【问题讨论】:
-
那不是 python,它是非常糟糕的伪代码。某些锁只在 2 个进程之间起作用
标签: operating-system synchronization semaphore producer-consumer