cpp实现读写锁以及写者优先的读写锁

本文首发于我的公众号:码农手札,主要介绍linux下c++开发的知识包括网络编程的知识同时也会介绍一些有趣的算法题,欢迎大家关注,利用碎片时间学习一些编程知识,冰冻三尺非一日之寒,让我们一起加油!

前言

学习操作系统的过程中又接触到了读写锁,这个东西很早就听说过,但是一直也没具体了解如何实现一个读写锁,今天既然正好看到了,不妨就试试来实现一下,通过锁和条件变量来实现读写锁以及写者优先的读写锁

读写锁

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
#include <mutex>
#include <condition_variable>
class WfirstRWLock
{
public:
WfirstRWLock() = default;
~WfirstRWLock() = default;
public:
void lock_read()
{
std::unique_lock<std::mutex> ulk(counter_mutex);
cond_r.wait(ulk, [=]() -> bool { return write_cnt == 0; });
++read_cnt;
}
void lock_write()
{
std::unique_lock<std::mutex> ulk(counter_mutex);
cond_w.wait(ulk, [=]()->bool {return read_cnt == 0 && !inwriteflag; });
++write_cnt;
inwriteflag = true;
}
void release_read()
{
std::unique_lock<std::mutex> ulk(counter_mutex);
if (--read_cnt == 0)
{
///if no writer is waiting, this func @notify_one will do nothing
cond_w.notify_one();
}
}
void release_write()
{
std::unique_lock<std::mutex> ulk(counter_mutex);
if (--write_cnt == 0)
{
cond_r.notify_all();
}
else
{
cond_w.notify_one();
}
inwriteflag = false;
}
private:
volatile size_t read_cnt{ 0 };
volatile size_t write_cnt{ 0 };
volatile bool inwriteflag{ false };
std::mutex counter_mutex;
std::condition_variable cond_w;
std::condition_variable cond_r;
};

上面的代码使用来一些c++11中的特性,使得代码十分简单,也不难理解

写者优先的读写锁

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
#include <condition_variable>
#include <mutex>

class WfirstRWLock {
public:
WfirstRWLock() = default;
~WfirstRWLock() = default;

public:
void lock_read() {
std::unique_lock<std::mutex> ulk(counter_mutex);
cond_r.wait(ulk, [=]() -> bool { return write_cnt == 0; });
++read_cnt;
}
void lock_write() {
std::unique_lock<std::mutex> ulk(counter_mutex);
++write_cnt;
cond_w.wait(ulk, [=]() -> bool { return read_cnt == 0 && !inwriteflag; });
inwriteflag = true;
}
void release_read() {
std::unique_lock<std::mutex> ulk(counter_mutex);
if (--read_cnt == 0 && write_cnt > 0) {
cond_w.notify_one();
}
}
void release_write() {
std::unique_lock<std::mutex> ulk(counter_mutex);
if (--write_cnt == 0) {
cond_r.notify_all();
} else {
cond_w.notify_one();
}
inwriteflag = false;
}

private:
volatile size_t read_cnt{0};
volatile size_t write_cnt{0};
volatile bool inwriteflag{false};
std::mutex counter_mutex;
std::condition_variable cond_w;
std::condition_variable cond_r;
};

这个代码其实也很简单,只是在读者被唤醒的时候判断了写者的数量是否为0,如果是0的话才会被唤醒,如果不是0的话,意味着有写者正在等待,那么这个读者就会继续睡眠,直到写者的数量为0,这样我们就实现了一个写者优先的读写锁。

再提供一个对于读写锁的封装,实现RAII

读写锁封装

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
template <typename _RWLockable> class unique_writeguard {
public:
explicit unique_writeguard(_RWLockable &rw_lockable)
: rw_lockable_(rw_lockable) {
rw_lockable_.lock_write();
}
~unique_writeguard() { rw_lockable_.release_write(); }

private:
unique_writeguard() = delete;
unique_writeguard(const unique_writeguard &) = delete;
unique_writeguard &operator=(const unique_writeguard &) = delete;

private:
_RWLockable &rw_lockable_;
};
template <typename _RWLockable> class unique_readguard {
public:
explicit unique_readguard(_RWLockable &rw_lockable)
: rw_lockable_(rw_lockable) {
rw_lockable_.lock_read();
}
~unique_readguard() { rw_lockable_.release_read(); }

private:
unique_readguard() = delete;
unique_readguard(const unique_readguard &) = delete;
unique_readguard &operator=(const unique_readguard &) = delete;

private:
_RWLockable &rw_lockable_;
};

这样我们就完成了一个完整的读写锁,实现起来并不是很麻烦,只是需要一些简单c++11特性以及小技巧,比如使用两个条件变量cond_w和cond_r将读写线程等待的信号分开,这样在读多写少的条件下就会很方便的唤醒写线程,减少唤醒线程的数量也就减小了线程的切换,减小了开销。

参考博客:
c++中写者优先的读写锁实现