2012-01-24 34 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.

一般來說,維基百科上的代碼經常會在很長一段時間內被很多人編輯,因此很容易在其中引入錯誤。