MVault 1.0.0
Simple c++ database
Loading...
Searching...
No Matches
ReadWriteMutex.h
1#pragma once
2
3#include <mutex>
4#include <atomic>
5#include <thread>
6#include <iostream>
7#include <condition_variable>
8
9namespace mvlt
10{
20 {
21 private:
22 std::mutex WriteMutex, ReadMutex;
23 std::atomic_int ReadCounter;
24 std::atomic_bool IsCondVarWaiting;
25 std::condition_variable Cv;
26
27 // A variable for switching the operation of the mutex. It is needed so that you can disable the mutex during inheritance
28 bool IsActive = true;
29
30 public:
32 ReadWriteMutex() noexcept;
33
35 void Disable() noexcept;
36
43 void ReadLock() noexcept;
44
46 void ReadUnlock() noexcept;
47
55 void WriteLock() noexcept;
56
58 void WriteUnlock() noexcept;
59 };
60
71 {
72 private:
73 ReadWriteMutex Rwmx;
74
75 // A variable for switching the operation of the mutex. It is needed so that you can disable the mutex during inheritance
76 bool IsActive = true;
77
78 public:
80 void Disable() noexcept;
81
90 void ReadLock() noexcept;
91
93 void ReadUnlock() noexcept;
94
103 void WriteLock() noexcept;
104
110 void WriteUnlock() noexcept;
111 };
112
122 template <class Mutex>
124 {
125 private:
126 Mutex* Mtx = nullptr;
127
128 public:
131
138
145
154 {
155 Mtx->ReadLock();
156 }
157
164 ReadLock<Mutex>& operator=(const ReadLock<Mutex>& other) noexcept = delete;
165
173
180 {
181 Mtx->ReadUnlock();
182 }
183 };
184
194 template <class Mutex>
196 {
197 private:
198 Mutex* Mtx = nullptr;
199
200 public:
203
210
217
226 {
227 Mtx->WriteLock();
228 }
229
237
245
252 {
253 Mtx->WriteUnlock();
254 }
255 };
256}
Iterator class for all library maps.
Definition Map.h:18
A class for blocking a stream for reading.
Definition ReadWriteMutex.h:124
~ReadLock() noexcept
Destructor.
Definition ReadWriteMutex.h:179
ReadLock< Mutex > & operator=(ReadLock< Mutex > &&other) noexcept=delete
Deleted move assignment operator.
ReadLock() noexcept=delete
Deleted default constructor.
ReadLock< Mutex > & operator=(const ReadLock< Mutex > &other) noexcept=delete
Deleted assignment operator.
A class for synchronizing threads.
Definition ReadWriteMutex.h:20
ReadWriteMutex() noexcept
Default constructor.
Definition ReadWriteMutex.cpp:5
void ReadLock() noexcept
A method for locking a section of code for reading.
Definition ReadWriteMutex.cpp:16
void WriteLock() noexcept
A method for locking a section of code for writing.
Definition ReadWriteMutex.cpp:43
void WriteUnlock() noexcept
A method for unlocking a section of code for writing.
Definition ReadWriteMutex.cpp:60
void ReadUnlock() noexcept
A method for unlocking a section of code for reading.
Definition ReadWriteMutex.cpp:28
void Disable() noexcept
A function to disable the mutex. It is needed so that you can disable the mutex during inheritance.
Definition ReadWriteMutex.cpp:11
A class for synchronizing threads.
Definition ReadWriteMutex.h:71
void ReadLock() noexcept
A method for locking a section of code for reading.
Definition ReadWriteMutex.cpp:76
void WriteUnlock() noexcept
A method for unlocking a section of code for writing.
Definition ReadWriteMutex.cpp:120
void WriteLock() noexcept
A method for locking a section of code for writing.
Definition ReadWriteMutex.cpp:104
void Disable() noexcept
A function to disable the mutex. It is needed so that you can disable the mutex during inheritance.
Definition ReadWriteMutex.cpp:71
void ReadUnlock() noexcept
A method for unlocking a section of code for reading.
Definition ReadWriteMutex.cpp:90
A class for blocking a stream for writing.
Definition ReadWriteMutex.h:196
~WriteLock() noexcept
Destructor.
Definition ReadWriteMutex.h:251
WriteLock() noexcept=delete
Deleted default constructor.
WriteLock< Mutex > & operator=(WriteLock< Mutex > &&other) noexcept=delete
Deleted move assignment operator.
WriteLock< Mutex > & operator=(const WriteLock< Mutex > &other) noexcept=delete
Deleted assignment operator.