0
我想知道如何解决生产者/消费者问题,但使用2个不同的消费者,我也需要知道如何使用严格的交替来解决它。解决生产者/消费者问题的算法
我做了如下算法1个生产者和1名消费者
producer()
{
while(true)
{
if i == N //full buffer
turn = 1
while turn <> 0
{
// nothing
}
produceitem(&item)//produce the item
insertitem(item, buffer)//insert the item in the buffer
turn = 1
//process zone
}
}
consumer()
{
while(true)
{
if i == 0 //empty buffer
turn = 0
while turn <> 1
{
// nothing
}
consumeitem(&item)
deleteitem(buffer)//erase the item from buffer
turn = 0
//process zone
}
}
使用那种“伪代码”我想知道,以解决同样的问题(如果最后是OK)2消费者。
第二个函数肯定应该命名为'consumer()'。 –