File was renamed from src/read_write_lock.h |
| | |
| | | #ifndef _CLOSE_LOCK_H_ |
| | | #define _CLOSE_LOCK_H_ |
| | | #ifndef _PREAD_WRITE_LOCK_H_ |
| | | #define _PREAD_WRITE_LOCK_H_ |
| | | |
| | | #include "usg_common.h" |
| | | #include "psem.h" |
| | | class ReadWriteLock { |
| | | class PReadWriteLock { |
| | | private: |
| | | unsigned int readCount = 0; |
| | | sem_t countMutex; |
| | |
| | | |
| | | |
| | | public: |
| | | ReadWriteLock() { |
| | | PReadWriteLock() { |
| | | if (sem_init(&countMutex, 1, 1) == -1) |
| | | err_exit(errno, "PReadWriteLock sem_init"); |
| | | |
| | | if (sem_init(&writeMutex, 1, 1) == -1) |
| | | err_exit(errno, "PReadWriteLock sem_init"); |
| | | } |
| | | |
| | | void lockRead() { |
| | | //readCount是共享变量,所以需要实现一个锁来控制读写 |
| | | //synchronized(ReadWriteLock.class){} |
| | | //synchronized(PReadWriteLock.class){} |
| | | psem_wait(&countMutex); |
| | | //只有是第一个读者,才将写锁加锁。其他的读者都是进行下一步 |
| | | if(readCount == 0){ |