实现锁和条件变量

实现锁和条件变量,这个基本都是在参考别人的代码,然后自己慢慢理解.突然觉得自己好弱阿,不但写不出代码,看别人写的都要看半天才明白....打击颇大,要继续加油了.不然以后只好去中关村卖电脑了.

1.实现锁
锁和信号量的区别在于锁只有两个状态,0或者1,同一时间只能有1个线程进入临界区,所以也叫互斥量,还有一个区别时,为锁添加了占用锁的当前线程属性。
修改synch.h文件,为Lock添加两个私有属性,拥有锁的线程和互斥信号量.

    Thread* holder;            // 拥有锁的线程
    Semaphore* lock;            // 互斥信号

实现方法
在构造函数中初始化该变量.信号量只能有两个状态BUSY 和 FREE
    holder = NULL;
    lock = new Semaphore(name,1);

加锁和解锁的操作都是原子的,必须屏蔽中断.执行P操作,并设置当前占用线程
加锁操作
void Lock::Acquire()
{
    IntStatus oldLevel = interrupt->SetLevel(IntOff);  // disable interrupts       
    lock->P();                                                                     // procure the semaphore
    holder = currentThread;                                          
    (void) interrupt->SetLevel(oldLevel); // re-enable interrupts
}
解锁操作,当前占用锁的进程为空,释放锁
void Lock::Release()
{
       IntStatus oldLevel = interrupt->SetLevel(IntOff);  // disable interrupts
       ASSERT(currentThread == holder);        
       holder = NULL;                        
       lock->V();                            
    (void) interrupt->SetLevel(oldLevel);
}

2.实现条件变量
条件变量通常和锁一起使用,所有对条件变量的操作必须保证当前线程占有互斥锁。
所以在条件变量中声明一个锁和一个等待该条件变量的队列。
    List* queue;            //等待条件变量的线程队列
    Lock* lock;                //用来判断锁是否被当线程占用
构造函数初始化:
    queue = new List;
    lock = NULL;
当执行Wait操作时,当他不能获得期待的一个条件变量时,线程进入等待状态,原子性的调用并解锁它锁持有的互斥量,
当另一个线程向它发送信号时,它可以继续执行。

void Condition::Wait(Lock* conditionLock)
{
    IntStatus oldLevel = interrupt->SetLevel(IntOff);

    ASSERT(conditionLock->isHeldByCurrentThread()); 
    if(queue->IsEmpty()) {
    lock = conditionLock;                                                  // 当等待队列为空时,绑定该条件变量和互斥锁。
    }
    ASSERT(lock == conditionLock);
    queue->Append(currentThread);                             //当前线程添加到该条件变量的等待队列中
    conditionLock->Release();                                        //释放当前锁,使得其他线程可以进入临界区
    currentThread->Sleep();                                            //当前线程进入等待状态
    conditionLock->Acquire();                                         //当其他线程发送一个信号后,当前线程wake up,并获得锁
    (void) interrupt->SetLevel(oldLevel);
}

//发送信号,从等待队列中获得一个线程并启动
void Condition::Signal(Lock* conditionLock)
{
    Thread *nextThread;
    IntStatus oldLevel = interrupt->SetLevel(IntOff);

    ASSERT(conditionLock->isHeldByCurrentThread());
    if(!queue->IsEmpty()) {
    ASSERT(lock == conditionLock);
    nextThread = (Thread *)queue->Remove();
    scheduler->ReadyToRun(nextThread);     
    }
    (void) interrupt->SetLevel(oldLevel);
}

//广播,使等待队列中每一个线程都启动
void Condition::Broadcast(Lock* conditionLock)
{
    Thread *nextThread;
    IntStatus oldLevel = interrupt->SetLevel(IntOff);

    ASSERT(conditionLock->isHeldByCurrentThread());
    if(!queue->IsEmpty()) {
    ASSERT(lock == conditionLock);
    while(nextThread = (Thread *)queue->Remove()) {
        scheduler->ReadyToRun(nextThread, 0); 
    }
    }
    (void) interrupt->SetLevel(oldLevel);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值