linux 读写锁

本文详细介绍了Linux中读写锁pthread_rwlock_t的结构体定义及其在多线程编程中的作用,强调了加锁解锁的规则和死锁问题。通过示例解释了读写锁在读写操作中的行为,并提到了自旋锁作为对比,指出在适当场景下使用读写锁能提高并发性能。
摘要由CSDN通过智能技术生成

pthread_rwlock_t 结构体的定义:

struct
{
int __lock;
unsigned int __nr_readers;
unsigned int __readers_wakeup;
unsigned int __writer_wakeup;
unsigned int __nr_readers_queued;
unsigned int __nr_writers_queued;
/* FLAGS must stay at this position in the structure to maintain
binary compatibility. */
unsigned char __flags;
unsigned char __shared;
unsigned char __pad1;
unsigned char __pad2;
int __writer;
} __data;

endif
char __size[__SIZEOF_PTHREAD_RWLOCK_T];
long int __align;
} pthread_rwlock_t;

在多线程编程中,死锁问题是比较难定位的一种,通过调用栈我们查看此时的pthread_rwlock_t信息,对我们定位问题的帮助是很大的。

我们通常关注的字段有:

__nr_readers,

__nr_readers_queued,

__nr_writers_queued,

__writer

那这四个字段,那当我们线程加锁时,是怎么反应并记录到这个结构体中的呢?

当我们使用pthread_rwlock_rdlock()获取一次读锁时,__nr_rea

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值