C++11读写锁

网络摘抄的读写锁代码,先保存一下,之后抽时间验证一下,看代码应该是没有问题的。
摘抄自:https://blog.csdn.net/mymodian9612/article/details/52794980

#ifndef __WRITE_FIRST_RW_LOCK_H  
#define __WRITE_FIRST_RW_LOCK_H  

#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);  
        ++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;  
};  

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_;  
};  

#endif  
C11标准中提供了读写锁(read-write lock)的机制,可以在多线程环境下实现对共享资源的读写控制。读写锁可以分为读锁和写锁,读锁可以被多个线程同时持有,但是写锁只能被一个线程独占。读写锁的基本操作包括: 1. 初始化:pthread_rwlock_init() 2. 加读锁:pthread_rwlock_rdlock() 3. 加写锁:pthread_rwlock_wrlock() 4. 解锁:pthread_rwlock_unlock() 5. 销毁:pthread_rwlock_destroy() 下面是一个简单的使用读写锁的例子: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> pthread_rwlock_t rwlock; int shared_data = 0; void *reader(void *arg) { pthread_rwlock_rdlock(&rwlock); printf("Reader %ld: shared_data = %d\n", (long)arg, shared_data); pthread_rwlock_unlock(&rwlock); return NULL; } void *writer(void *arg) { pthread_rwlock_wrlock(&rwlock); shared_data++; printf("Writer %ld: shared_data = %d\n", (long)arg, shared_data); pthread_rwlock_unlock(&rwlock); return NULL; } int main() { pthread_t threads[6]; pthread_rwlock_init(&rwlock, NULL); for (long i = 0; i < 2; i++) { pthread_create(&threads[i], NULL, writer, (void *)i); } for (long i = 2; i < 6; i++) { pthread_create(&threads[i], NULL, reader, (void *)i); } for (int i = 0; i < 6; i++) { pthread_join(threads[i], NULL); } pthread_rwlock_destroy(&rwlock); return 0; } ``` 在这个例子中,共有两个写线程和四个读线程。写线程负责增加共享数据的值,读线程负责读取共享数据的值。在写线程加写锁的时候,读线程会被阻塞;在读线程加读锁的时候,其他读线程可以同时持有读锁,但是写线程会被阻塞。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值