Samchon Framework for CPP  1.0.0
UniqueReadLock.hpp
1 #pragma once
2 
3 #include <samchon/library/RWMutex.hpp>
4 
5 namespace samchon
6 {
7 namespace library
8 {
38  {
39  private:
43  const RWMutex *rw_mutex;
44 
48  bool locked;
49 
50  public:
51  /* -----------------------------------------------------------
52  CONSTRUCTORS
53  ----------------------------------------------------------- */
60  UniqueReadLock(const RWMutex &rw_mutex, bool lock = true)
61  {
62  this->rw_mutex = &rw_mutex;
63  this->locked = false;
64 
65  if (lock == true)
66  this->lock();
67  };
68 
79  UniqueReadLock(const UniqueReadLock &) = delete;
80 
87  {
88  //MOVE
89  this->rw_mutex = obj.rw_mutex;
90  this->locked = obj.locked;
91 
92  //TRUNCATE
93  obj.rw_mutex = nullptr;
94  obj.locked = false;
95  };
96 
102  {
103  if (locked == true)
104  rw_mutex->readUnlock();
105  }
106 
107  /* -----------------------------------------------------------
108  LOCKERS
109  ----------------------------------------------------------- */
113  void lock() const
114  {
115  if (locked == true)
116  return;
117 
118  rw_mutex->readLock();
119  (bool&)locked = true;
120  };
121 
125  void unlock() const
126  {
127  if (locked == false)
128  return;
129 
130  rw_mutex->readUnlock();
131  (bool&)locked = false;
132  };
133 
137  //auto tryLock() const -> bool;
138  };
139 };
140 };
void readUnlock() const
Unlock of read.
Definition: RWMutex.hpp:90
void readLock() const
Lock on read.
Definition: RWMutex.hpp:67
UniqueReadLock(const RWMutex &rw_mutex, bool lock=true)
Construct from mutex.
~UniqueReadLock()
Default Destructor.
void lock() const
Lock on read.
void unlock() const
Unlock of read.
Unique lock for reading.
UniqueReadLock(UniqueReadLock &&obj)
Move Constructor.
bool locked
Whether the mutex was locked by UniqueLock.
const RWMutex * rw_mutex
Managed mutex.