Samchon Framework for CPP  1.0.0
UniqueWriteLock.hpp
1 #pragma once
2 
3 #include <samchon/library/RWMutex.hpp>
4 
5 namespace samchon
6 {
7 namespace library
8 {
40  {
41  private:
46 
50  bool locked;
51 
52  public:
53  /* -----------------------------------------------------------
54  CONSTRUCTORS
55  ----------------------------------------------------------- */
62  UniqueWriteLock(RWMutex &rw_mutex, bool lock = true)
63  {
64  this->rw_mutex = &rw_mutex;
65  this->locked = false;
66 
67  if (lock == true)
68  this->lock();
69  };
70 
81  UniqueWriteLock(const UniqueWriteLock &) = delete;
82 
89  {
90  //MOVE
91  this->rw_mutex = obj.rw_mutex;
92  this->locked = obj.locked;
93 
94  //TRUNCATE
95  obj.rw_mutex = nullptr;
96  obj.locked = false;
97  };
98 
106  {
107  if (locked == true)
108  rw_mutex->writeUnlock();
109  };
110 
111  /* -----------------------------------------------------------
112  LOCKERS
113  ----------------------------------------------------------- */
117  void lock()
118  {
119  if (locked == true)
120  return;
121 
122  rw_mutex->writeLock();
123  locked = true;
124  };
125 
129  void unlock()
130  {
131  if (locked == false)
132  return;
133 
134  rw_mutex->writeUnlock();
135  locked = false;
136  };
137 
141  //auto tryLock() -> bool;
142  };
143 };
144 };
UniqueWriteLock(UniqueWriteLock &&obj)
Move Constructor.
void writeLock()
Lock on writing.
Definition: RWMutex.hpp:117
UniqueWriteLock(RWMutex &rw_mutex, bool lock=true)
Construct from mutex.
Unique lock for writing.
void unlock()
Unlock on writing.
bool locked
Whether the mutex was locked by UniqueLock.
RWMutex * rw_mutex
Managed mutex.
void writeUnlock()
Unlock on writing.
Definition: RWMutex.hpp:130