type producer-consumer=monitor
Var in,out,count:integer;
buffer:array[0,…,n-1] of item;
notfull, notempty:condition;
procedure entry put(item)
begin
if count≥n then notfull.wait;
buffer(in)∶=nextp;
in∶=(in+1) mod n;
count∶=count+1;
if notempty.queue then notempty.signal;
end
procedure entry get(item)
begin
if count≤0 then notempty.wait;
nextc∶=buffer(out);
out∶=(out+1) mod n;
count∶=count-1;
if notfull.quene then notfull.signal;
end
begin in∶=out∶=0; count∶=0 end
温馨提示:内容为网友见解,仅供参考