来自维基百科的生产者 - 消费者问题:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题 - 为什么生产者up(fillCount) //the consumer may not finish before the producer在while循环之后.该计划何时到达,为什么需要它?
我认为代码没有这种意义.循环永远不会结束,因此无法触及相关的行.
该代码最初并未包含该行,并于2009年3月由匿名编辑添加.我现在删除了这一行.
一般来说,维基百科上的代码经常被很多人编辑很长一段时间,所以很容易将错误引入其中.