Linux之线程-读写锁pthread_rwlock_t

**主要内容:
(1)互斥量及如何使用
(2)什么是死锁,如何解决
(3)什么是读写锁,如何使用**
(4)条件变量实现的生产消费者模型
(5)信号量实现的生产消费者模型

3.读写锁

特点:读共享,写独占,写优先级高。读写锁仍然是一把锁,有不同的状态:未加锁、读锁和写锁。

场景:适合读的线程较多的情况

  1. 线程A加写锁成功,线程B请求读锁。--线程B阻塞
  2. 线程A持有读锁,线程B请求写锁。--线程B阻塞
  3. 线程A持有读锁,线程B请求读锁。--B加锁成功
  4. 线程A持有读锁,然后线程B请求读锁,然后线程C请求写锁。--BC阻塞--A释放,C加锁--C释放B加锁
  5. 线程A持有写锁,然后线程B请求读锁,然后线程C请求写锁。--BC阻塞--A释放,C加锁--C释放B加锁

函数原型:#include <pthread.h>

(1)读写锁初始化

  • int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,const pthread_rwlockattr_t *restrict attr);
  • pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;

(2)摧毁读写锁

  • int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

(3)加读锁

  • int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);  //成功返回0,错误返回an error number shall be returned to indicate the error.

  •  int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);  //同上

(4)加写锁

  • int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);

(5)释放锁

  • int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

示例:

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>

pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;  //读写锁初始化
int beginnum = 1000;

void *thr_write(void *arg)
{
    while (1)
    {
        pthread_rwlock_wrlock(&rwlock);
        printf("------%s-----self----%lu---beginnum----%d\n",__FUNCTION__,pthread_self(),++beginnum);
        usleep(2000); //模拟占用时间
        pthread_rwlock_unlock(&rwlock);
        usleep(4000);
    }
    
    return NULL;
}

void *thr_read(void *arg)
{
    while (1)
    {
        pthread_rwlock_rdlock(&rwlock);
        printf("------%s-----self----%lu---beginnum----%d\n",__FUNCTION__,pthread_self(),beginnum);
        usleep(2000); //模拟占用时间
        pthread_rwlock_unlock(&rwlock);
        usleep(2000);
    }

    return NULL;

}

int main()
{
    int n =8, i = 0;
    pthread_t tid[8];  //5-read, 3-write
    for(i = 0; i < 5; i++)
    {
        pthread_create(&tid[i],NULL,thr_read,NULL);
    }

    for(i = 0; i < 3; i++)
    {
        pthread_create(&tid[i],NULL,thr_write,NULL);
    }

    for(i=0; i < 8; i++)
    {
        pthread_join(tid[i],NULL);
    }
    return 0;
}

输出:

~$ gcc rwlock.c -lpthread
~$ ./a.out
------thr_read-----self----140403159697152---beginnum----1000
------thr_read-----self----140403151304448---beginnum----1000
------thr_read-----self----140403126126336---beginnum----1000
------thr_read-----self----140403134519040---beginnum----1000
------thr_read-----self----140403142911744---beginnum----1000
------thr_write-----self----140403117733632---beginnum----1001
------thr_write-----self----140403109340928---beginnum----1002
------thr_write-----self----140403100948224---beginnum----1003
------thr_write-----self----140403117733632---beginnum----1004
------thr_write-----self----140403109340928---beginnum----1005
------thr_write-----self----140403100948224---beginnum----1006
------thr_write-----self----140403117733632---beginnum----1007
------thr_write-----self----140403109340928---beginnum----1008
------thr_write-----self----140403100948224---beginnum----1009
------thr_write-----self----140403117733632---beginnum----1010
------thr_write-----self----140403109340928---beginnum----1011
------thr_write-----self----140403100948224---beginnum----1012
------thr_write-----self----140403117733632---beginnum----1013

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值