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循環。該計劃何時到達,爲什麼需要?