MVault 0.0.1
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 public:
31 ReadWriteMutex() noexcept;
32
34 void Disable() noexcept;
35
42 void ReadLock() noexcept;
43
45 void ReadUnlock() noexcept;
46
54 void WriteLock() noexcept;
55
57 void WriteUnlock() noexcept;
58 };
59
70 {
71 private:
72 ReadWriteMutex Rwmx;
73
74 // A variable for switching the operation of the mutex. It is needed so that you can disable the mutex during inheritance
75 bool IsActive = true;
76 public:
77
79 void Disable() noexcept;
80
89 void ReadLock() noexcept;
90
92 void ReadUnlock() noexcept;
93
102 void WriteLock() noexcept;
103
109 void WriteUnlock() noexcept;
110 };
111
121 template <class Mutex>
123 {
124 private:
125 Mutex& Mtx;
126 public:
135 {
136 Mtx.ReadLock();
137 }
138
145 {
146 Mtx.ReadUnlock();
147 }
148 };
149
159 template <class Mutex>
161 {
162 private:
163 Mutex& Mtx;
164 public:
173 {
174 Mtx.WriteLock();
175 }
176
183 {
184 Mtx.WriteUnlock();
185 }
186 };
187}
Iterator class for all library maps.
Definition Map.h:18
A class for blocking a stream for reading.
Definition ReadWriteMutex.h:123
~ReadLock()
Destructor.
Definition ReadWriteMutex.h:144
ReadLock(Mutex &mtx)
Constructor.
Definition ReadWriteMutex.h:134
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:42
void WriteUnlock() noexcept
A method for unlocking a section of code for writing.
Definition ReadWriteMutex.cpp:59
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:70
void ReadLock() noexcept
A method for locking a section of code for reading.
Definition ReadWriteMutex.cpp:75
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:103
void Disable() noexcept
A function to disable the mutex. It is needed so that you can disable the mutex during inheritance.
Definition ReadWriteMutex.cpp:70
void ReadUnlock() noexcept
A method for unlocking a section of code for reading.
Definition ReadWriteMutex.cpp:89
A class for blocking a stream for writing.
Definition ReadWriteMutex.h:161
~WriteLock()
Destructor.
Definition ReadWriteMutex.h:182
WriteLock(Mutex &mtx)
Constructor.
Definition ReadWriteMutex.h:172