欢迎来到天天文库
浏览记录
ID:11838052
大小:36.00 KB
页数:5页
时间:2018-07-14
《实现锁和条件变量》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
用Sleep来实现锁和条件变量//Dummyfunctions--sowecancompileourlaterassignments//Note--withoutacorrectimplementationofCondition::Wait(),//thetestcaseinthenetworkassignmentwon'twork!Lock::Lock(char*debugName){name=debugName;lock=FALSE;holder=NULL;queue=newList;}Lock::~Lock(){deletequeue;}voidLock::Acquire(){ASSERT(!isHeldByCurrentThread());//如果当前线程没有获得锁IntStatusoldLevel=interrupt->SetLevel(IntOff);//disableinterruptswhile(lock)//locknotavailable{queue->Append((void*)currentThread);//sogotosleepcurrentThread->Sleep();}lock=TRUE;//lockavailable,//consumeitsvalueholder=currentThread;(void)interrupt->SetLevel(oldLevel);//re-enableinterrupts}voidLock::Release(){Thread*thread;ASSERT(isHeldByCurrentThread());//如果表达式为假,则终止程序运行IntStatusoldLevel=interrupt->SetLevel(IntOff);thread=(Thread*)queue->Remove();if(thread!=NULL)//makethreadready,consumingtheVimmediatelyscheduler->ReadyToRun(thread);lock=FALSE;holder=NULL;(void)interrupt->SetLevel(oldLevel); }boolLock::isHeldByCurrentThread(){return(holder==currentThread);}Condition::Condition(char*debugName){name=debugName;queue=newList;}Condition::~Condition(){deletequeue;}voidCondition::Wait(Lock*conditionLock){ASSERT(conditionLock->isHeldByCurrentThread());IntStatusoldLevel=interrupt->SetLevel(IntOff);//disableinterruptsconditionLock->Release();queue->Append((void*)currentThread);//sogotosleepcurrentThread->Sleep();(void)interrupt->SetLevel(oldLevel);//re-enableinterruptsconditionLock->Acquire();}voidCondition::Signal(Lock*conditionLock){Thread*thread;ASSERT(conditionLock->isHeldByCurrentThread());thread=(Thread*)queue->Remove();if(thread!=NULL)//makethreadready,consumingtheVimmediatelyscheduler->ReadyToRun(thread);}voidCondition::Broadcast(Lock*conditionLock){Thread*thread;ASSERT(conditionLock->isHeldByCurrentThread()); do{thread=(Thread*)queue->Remove();if(thread!=NULL)//makethreadready,consumingtheVimmediatelyscheduler->ReadyToRun(thread);}while(thread!=NULL);} 用信号量来实现条件变量//Dummyfunctions--sowecancompileourlaterassignments//Note--withoutacorrectimplementationofCondition::Wait(),//thetestcaseinthenetworkassignmentwon'twork!Lock::Lock(char*debugName){name=debugName;holder=NULL;semlock=newSemaphore(debugName,1);}Lock::~Lock(){deletesemlock;}voidLock::Acquire(){ASSERT(!isHeldByCurrentThread());semlock->P();holder=currentThread;}voidLock::Release(){ASSERT(isHeldByCurrentThread());holder=NULL;semlock->V();}boolLock::isHeldByCurrentThread(){return(holder==currentThread);}Condition::Condition(char*debugName){name=debugName;semcond=newSemaphore(debugName,0);count=0;}Condition::~Condition() {deletesemcond;}voidCondition::Wait(Lock*conditionLock){ASSERT(conditionLock->isHeldByCurrentThread());count++;conditionLock->Release();semcond->P();conditionLock->Acquire();}voidCondition::Signal(Lock*conditionLock){ASSERT(conditionLock->isHeldByCurrentThread());if(count>0){semcond->V();count--;}}voidCondition::Broadcast(Lock*conditionLock){ASSERT(conditionLock->isHeldByCurrentThread());while(count>0){semcond->V();count--;}}
此文档下载收益归作者所有
举报原因
联系方式
详细说明
内容无法转码请点击此处