Yanyg - Software Engineer

读写锁

目录

1 RWLock

1.1 WriteFirst

  • 特定位标1表示有写在等待,读排队等待;
  • 多个写通过写位Xchg实现;

1.2 ReadFirst

  • 先自增锁计数;
  • 只要有写锁就等待,否则完成;
  • 写锁检查到锁计数不为0等待,否则Xchg设置写锁位;

2 SequenceLock

// Writer:
{
    ScopedSeqLockWriter lock_(&mSeqLock);
    // do something
}

// Reader
{
    ScopedSeqLockReader lock_(&mSeqLock);
    {
        // do something
    } while (lock_.NeedRetry());
}

3 Mutex

4 SpinLock

5 Atomic

6 CountDown

7 Future

8 SyncClosure

9 RCU