10
从
Wikipedia采取
生产者 - 消费者问题:生产者 - 消费者与sempahores
semaphore mutex = 1
semaphore fillCount = 0
semaphore emptyCount = BUFFER_SIZE
procedure producer() {
while (true) {
item = produceItem()
down(emptyCount)
down(mutex)
putItemIntoBuffer(item)
up(mutex)
up(fillCount)
}
up(fillCount) //the consumer may not finish before the producer.
}
procedure consumer() {
while (true) {
down(fillCount)
down(mutex)
item = removeItemFromBuffer()
up(mutex)
up(emptyCount)
consumeItem(item)
}
}
我的问题 - 为什么生产者有up(fillCount) //the consumer may not finish before the producer
后while循环。该计划何时到达,为什么需要?