-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReaderWriter.cpp
55 lines (50 loc) · 1.09 KB
/
ReaderWriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* C++11 lock manages a mutex object. Locks it upon construction, unlocks it
* upon destruction. It also means the it unlocks the mutex upon exceptions.
* lock_guard cannot be unlocked. Unlock it by getting it out of scope.
* unique_lock can be explicitly unlocked.
*/
#include <condition_variable>
#include <mutex>
class ReaderWriter
{
public:
ReaderWriter(): readers(0), writers(0), active_writers(0) { }
void reader();
void writer();
private:
int readers, writers;
int active_writers;
std::mutex mtx;
std::condition_variable readersQ, writersQ;
};
void ReaderWriter::reader()
{
std::unique_lock<std::mutex> lck(mtx);
while(writers > 0)
readersQ.wait(lck);
readers++;
lck.unlock();
// read here
lck.lock();
if(--readers == 0)
writersQ.notify_one();
lck.unlock();
}
void ReaderWriter::writer()
{
std::unique_lock<std::mutex> lck(mtx);
writers++;
while(readers > 0 or writers > 0)
writersQ.wait(lck);
active_writers++;
lck.unlock();
// write here
lck.lock();
active_writers--;
if(--writers != 0)
writersQ.notify_one();
else
readersQ.notify_all();
lck.unlock();
}