生产者-消费者问题.
# define N 50
monitor Pro_Con
{
Item buffer[N];
int in;
int out;
int count;
condition NotFull;
condition NotEmpty;
void Put(Item x)
{
if(count>=N)
{
NotFull.wait();
}
buffer[in]=x;
in=(in+1)%N;
count++;
NotEmpty.signal();
}
void Get(Item x)
{
if(count<=0)
{
NotEmpty.wait();
}
x=buffer[out];
out=(out+1)%N;
count--;
NotFull.signal();
}
Initialization()
{
in=0;
out=0;
count=0;
}
}
void Producer()
{
Item item;
while(true)
{
item=new Item();
Pro_Con.Put(item);
}
}
void Consumer()
{
Item item;
while(true)
{
Pro_Con.Get(item);
}
}
哲学家就餐问题(防饿死).