RW锁 读写锁,也叫共享独占锁
互斥量 要么是锁住状态,要么是不加锁状态,而且一次只有一个线程可以对其加锁。
读写锁可以有三种状态,读模式下加锁状态,写模式下加锁状态,不加锁状态。一次只有一个线程可以占有写模式的读写锁,但是多个线程可以同时占有读模式的读写锁。
和互斥量一样,读写锁在使用之前必须初始化,在释放他们底层的内存前必须销毁。
要在读模式下锁定读写锁,需要调用pthread_rwlock_rdlock;
要在写模式下锁定读写锁,需要调用pthread_rwlock_wrlock;
不管以何种方式锁住读写锁,都可以调用pthread_rwlock_unlock 进行解锁。
在读操作远远高于写操作的时候,使用读写锁才可能改善性能。
下面给出一个cpp 版本的 rw_lock
Rwlock.h
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">class RwLock {NOCOPY_CLASS(RwLock);<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span>:<span class="hljs-title" style="box-sizing: border-box;">RwLock</span>();<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">virtual</span> ~RwLock();<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> ReadLock();<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> WriteLock();<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> Unlock();<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> ReadUnlock();<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> WriteUnlock();<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> IsReadLocked();<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> IsWriteLocked();<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span>:pthread_rwlock_t lock_; };Rw_lock.cc</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>
<code class="hljs d has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">RwLock::RwLock() {pthread_rwlock_init(&lock_, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>); }RwLock::~RwLock() {pthread_rwlock_destroy(&lock_); }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> RwLock::ReadLock() {pthread_rwlock_rdlock(&lock_); }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> RwLock::WriteLock() {pthread_rwlock_wrlock(&lock_); }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> RwLock::Unlock() {<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">assert</span>(IsReadLocked() || IsWriteLocked());pthread_rwlock_unlock(&lock_); }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> RwLock::ReadUnlock() {<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">assert</span>(IsReadLocked());pthread_rwlock_unlock(&lock_); }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> RwLock::WriteUnlock() {<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">assert</span>(IsWriteLocked());pthread_rwlock_unlock(&lock_); }<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">bool</span> RwLock::IsReadLocked() {<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> lock_.__data.__nr_readers != <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">bool</span> RwLock::IsWriteLocked() {<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> lock_.__data.__writer != <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li></ul>
同样,不建议直接使用成员函数来上锁,需要写一个ReadLockGuard模版来更好的使用锁,防止出错。
<code class="hljs haskell has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-title" style="box-sizing: border-box;">template</span> <typename <span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">LockTye</span>> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">ReadLockGuard</span> {<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">NOCOPY_CLASS</span><span class="hljs-container" style="box-sizing: border-box;">(<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">ReadLockGuard</span>)</span>;public:<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">ReadLockGuard</span><span class="hljs-container" style="box-sizing: border-box;">(<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">LockType</span> * <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">lock</span>)</span>:lock_<span class="hljs-container" style="box-sizing: border-box;">(<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">lock</span>)</span> {lock-><span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">ReadLock</span><span class="hljs-container" style="box-sizing: border-box;">()</span>;}~<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">ReadLockGuard</span><span class="hljs-container" style="box-sizing: border-box;">()</span> {lock_-><span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">ReadUnlock</span><span class="hljs-container" style="box-sizing: border-box;">()</span>;}private:<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">LockType</span>* lock_; };template <typename <span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">LockType</span>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">WriteLockGuard</span> {<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">NOCOPY_CLASS</span><span class="hljs-container" style="box-sizing: border-box;">(<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">WriteLockGuard</span>)</span>;public:<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">WriteLockGuard</span><span class="hljs-container" style="box-sizing: border-box;">(<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">LockType</span>* <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">lock</span>)</span>: lock_<span class="hljs-container" style="box-sizing: border-box;">(<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">lock</span>)</span> {lock_-><span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">WriteLock</span><span class="hljs-container" style="box-sizing: border-box;">()</span>;}~<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">WriteLockGuard</span><span class="hljs-container" style="box-sizing: border-box;">()</span> {lock_-><span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">WriteUnlock</span><span class="hljs-container" style="box-sizing: border-box;">()</span>;}private:<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">LockType</span>* lock_; };</span></code>