2012-01-24 44 views
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循环。该计划何时到达,为什么需要?

回答

5

我认为这样的代码没有意义。循环永远不会结束,所以有问题的线路永远都不会到达。

该代码原本不包含该行,它是added by an anonymous editor in March 2009I removed that line now.

一般来说,维基百科上的代码经常会在很长一段时间内被很多人编辑,因此很容易在其中引入错误。