Skip to content

Instantly share code, notes, and snippets.

@romanbsd
Created June 24, 2020 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romanbsd/0c136a565535762775f5649831e243c7 to your computer and use it in GitHub Desktop.
Save romanbsd/0c136a565535762775f5649831e243c7 to your computer and use it in GitHub Desktop.
#ifndef __RWLOCK_H
#define __RWLOCK_H
#include <pthread.h>
class rwlock {
public:
rwlock() {
pthread_rwlock_init(&lock_, nullptr);
}
rwlock(const rwlock &) = delete;
rwlock &operator=(const rwlock &) = delete;
~rwlock() {
pthread_rwlock_destroy(&lock_);
}
void lock() {
wrlock();
}
void wrlock() {
pthread_rwlock_wrlock(&lock_);
}
void rdlock() {
pthread_rwlock_rdlock(&lock_);
}
void unlock() {
pthread_rwlock_unlock(&lock_);
}
private:
pthread_rwlock_t lock_;
};
class lock_rd_guard {
public:
explicit lock_rd_guard(rwlock &lock) : lock(lock) {
lock.rdlock();
}
~lock_rd_guard() {
lock.unlock();
}
private:
rwlock &lock;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment