实现锁和条件变量

实现锁和条件变量

ID:11838052

大小:36.00 KB

页数:5页

时间:2018-07-14

上传者:U-4186
实现锁和条件变量_第1页
实现锁和条件变量_第2页
实现锁和条件变量_第3页
实现锁和条件变量_第4页
实现锁和条件变量_第5页
资源描述:

《实现锁和条件变量》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

用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--;}}

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。
关闭