3,881
社区成员
发帖
与我相关
我的任务
分享
class A
{
Array arr[];//设所有操作为原子操作
int count;//设所有操作为原子操作
push(item)//线程A写入
{
arr.push(item);//写入
count++;//计数++
if(count==0)//如果队列中仅此一个资源
{
awake();//唤醒B继续来读
}
}
item pop()//线程B读取
{
count--;//计数--
if(count<0)//如果队列中目前没有资源
{
if(wait(100ms))//阻塞自己等待A唤醒或超时
{
arr.pop();//读取
}else
{
count++;
}
}else
{
arr.pop();//读取
}
}
}