Nhà sản xuất-tiêu dùng lấy từ Wikipedia:Nhà sản xuất-tiêu dùng với 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)
}
}
Câu hỏi của tôi - tại sao nhà sản xuất có up(fillCount) //the consumer may not finish before the producer
sau khi vòng lặp while. Khi nào chương trình sẽ đến đó và tại sao nó lại cần thiết?