算法:读写者问题

锁解析 :

  • RWMutex : 读-写互斥锁
  • WWMutex : 写-写互斥锁
  • WPriorityMutex = 1 : 写优先锁
  • WW-CMutex : 修改写者个数锁
  • RR-CMutex :修改读者个数锁
int : writeCount=0, readCount=0

void reader() {
    //读 抢锁
    P(WPriorityMutex)
    P(RWMutex)
    P(RR-CMutex)
        if (++readCount == 1) P(WWMutex)
    V(RR-CMutex)
    V(RWMutex)
    V(WPriorityMutex)

    //读操作。。。

    P(RR-CMutex)
        if (--readCount == 0) V(WWMutex)
    V(RR-CMutex)
}

void write() {
    P(WW-CMutex)
        if (++writeCount == 1) P(RWMutex)
    V(WW-CMutex)

    P(WWMutex)

    //写操作。。。

    V(WWMutex)

    P(WW-CMutex)
        if (--writeCount == 0) P(RWMutex)
    V(WW-CMutex)
}

你可能感兴趣的:(算法:读写者问题)