Ceph读写锁实现

2016-12-09  本文已影响0人  phenom

src/common/RWLock.h

RWLock使用了C++11新特性Deleted函数,禁用其拷贝构造函数与赋值操作符。

public:
  RWLock(const RWLock& other) = delete;
  const RWLock& operator=(const RWLock& other) = delete;

使用两个原子变量对读和写锁进行计数:

  mutable atomic_t nrlock, nwlock;

为了防止写进程被饿死,可使用PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP对读写锁进行初始化:

#if defined(PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP)
    if (prioritize_write) {
      pthread_rwlockattr_t attr;
      pthread_rwlockattr_init(&attr);
      // PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
      //   Setting the lock kind to this avoids writer starvation as long as
      //   long as any read locking is not done in a recursive fashion.
      pthread_rwlockattr_setkind_np(&attr,
          PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
      pthread_rwlock_init(&L, &attr);
    } else
#endif

http://stackoverflow.com/questions/2190090/how-to-prevent-writer-starvation-in-a-read-write-lock-in-pthreads

上一篇 下一篇

猜你喜欢

热点阅读