本文主要是介绍图解读写锁:pthead_rwlock_t,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Table of Contents
在glibc-2.9 pthreadtypes.h
pthread_rwlock_t
线程们对读写锁的竞争关系
过程详述
相关文章
在glibc-2.9 pthreadtypes.h
nptl\sysdeps\unix\sysv\linux\i386\bits 4477 5/28/2007 第119行中定义了结构体
pthread_rwlock_t
typedef union
{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 maintainbinary compatibility. */unsigned char __flags;unsigned char __shared;unsigned char __pad1;unsigned char __pad2;int __writer;} __data;char __size[__SIZEOF_PTHREAD_RWLOCK_T];long int __align;
} pthread_rwlock_t;
线程们对读写锁的竞争关系
过程详述
- 1.读线程lock;
- 2.读线程lock;
- 3.读线程lock;
- 4.写线程lock,此时有读线程lock此锁,写线程进入等待状态,等待所有读线程释放该锁;
- 5.读线程unlock;
- 6.读线程unlock;
- 7.读线程unlock;
- 8.所有读线程均释放了该锁,此时写线程获取该锁,从“等待”继续执行;
- 9.写线程继续执行;
- 10.写字段+1;
相关文章
《Linux线程同步(3): 读写锁(rwlock)》https://blog.csdn.net/dai_weitao/article/details/1752843;
这篇关于图解读写锁:pthead_rwlock_t的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!