Created
June 24, 2020 08:47
-
-
Save romanbsd/0c136a565535762775f5649831e243c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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