线程相关函数(5)-pthread_rwlock_rdlock(),pthread_rwlock_wrlock() 读写锁
读共享,写独占
pthread_rwlock_t
pthread_rwlock_init
pthread_rwlock_destroy
pthread_rwlock_rdlock
pthread_rwlock_wrlock
pthread_rwlock_tryrdlock
pthread_rwlock_trywrlock
pthread_rwlock_unlock
示例代码:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h> static int counter;
static pthread_rwlock_t rwlock; void *th_write(void *arg)
{
int t, i;
for (i=; i<; i++) {
pthread_rwlock_wrlock(&rwlock);
t = counter;
usleep();
printf("Write Thread(%x) counter=%d, ++counter=%d\n", (int)pthread_self(), t, ++counter);
pthread_rwlock_unlock(&rwlock);
usleep();
} } void *th_read(void *arg)
{
while() {
pthread_rwlock_rdlock(&rwlock);
printf("Read Thread(%x) counter=%d\n", (int)pthread_self(), counter);
pthread_rwlock_unlock(&rwlock);
usleep();
}
} int main()
{
int i;
pthread_t tid[]; pthread_rwlock_init(&rwlock, NULL);
for (i=; i<; i++)
pthread_create(&tid[i], NULL, th_write, NULL);
for (i=; i<; i++)
pthread_create(&tid[i+], NULL, th_read, NULL);
for (i=; i<; i++)
pthread_join(tid[i], NULL); pthread_rwlock_destroy(&rwlock);
return ;
}
运行结果:
Read Thread(f901e700) counter=0
Read Thread(f981f700) counter=0
Read Thread(f881d700) counter=0
Read Thread(fa020700) counter=0
Read Thread(fa821700) counter=0
Write Thread(fb022700) counter=0, ++counter=1
Write Thread(fb823700) counter=1, ++counter=2
Write Thread(fc024700) counter=2, ++counter=3
Write Thread(fb022700) counter=3, ++counter=4
Write Thread(fb823700) counter=4, ++counter=5
Write Thread(fc024700) counter=5, ++counter=6
Write Thread(fb022700) counter=6, ++counter=7
Write Thread(fb823700) counter=7, ++counter=8
Write Thread(fc024700) counter=8, ++counter=9
Write Thread(fb022700) counter=9, ++counter=10
Write Thread(fb823700) counter=10, ++counter=11
Write Thread(fc024700) counter=11, ++counter=12
Write Thread(fb022700) counter=12, ++counter=13
Write Thread(fb823700) counter=13, ++counter=14
Write Thread(fc024700) counter=14, ++counter=15
Read Thread(f901e700) counter=15
Read Thread(f981f700) counter=15
Read Thread(f881d700) counter=15
Read Thread(fa020700) counter=15
Read Thread(fa821700) counter=15
Read Thread(f901e700) counter=15
Read Thread(f981f700) counter=15
Read Thread(f881d700) counter=15
Read Thread(fa020700) counter=15
...
线程相关函数(5)-pthread_rwlock_rdlock(),pthread_rwlock_wrlock() 读写锁的更多相关文章
- JUC——线程同步锁(ReentrantReadWriteLock读写锁)
读写锁简介 所谓的读写锁值得是两把锁,在进行数据写入的时候有一个把“写锁”,而在进行数据读取的时候有一把“读锁”. 写锁会实现线程安全同步处理操作,而读锁可以被多个对象读取获取. 读写锁:ReadWr ...
- linux c 线程相关函数
线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程 一. pthread_creat ...
- Linux线程同步之读写锁(rwlock)
读写锁和互斥量(互斥锁)很类似,是另一种线程同步机制,但不属于POSIX标准,可以用来同步同一进程中的各个线程.当然如果一个读写锁存放在多个进程共享的某个内存区中,那么还可以用来进行进程间的同步, 和 ...
- 嵌入式 Linux线程同步读写锁rwlock示例
读写锁比mutex有更高的适用性,可以多个线程同时占用读模式的读写锁,但是只能一个线程占用写模式的读写锁.1. 当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞:2. ...
- Linux程序设计学习笔记----多线程编程线程同步机制之相互排斥量(锁)与读写锁
相互排斥锁通信机制 基本原理 相互排斥锁以排他方式防止共享数据被并发訪问,相互排斥锁是一个二元变量,状态为开(0)和关(1),将某个共享资源与某个相互排斥锁逻辑上绑定之后,对该资源的訪问操作例如以下: ...
- UNIX环境高级编程——线程同步之读写锁以及属性
读写锁和互斥量(互斥锁)很类似,是另一种线程同步机制,但不属于POSIX标准,可以用来同步同一进程中的各个线程.当然如果一个读写锁存放在多个进程共享的某个内存区中,那么还可以用来进行进程间的同步, 互 ...
- Linux:使用读写锁使线程同步
基础与控制原语 读写锁 与互斥量类似,但读写锁允许更高的并行性.其特性为:写独占,读共享. 读写锁状态: 一把读写锁具备三种状态: 1. 读模式下加锁状态 (读锁) 2. 写模式下加锁 ...
- linux线程间同步(1)读写锁
读写锁比mutex有更高的适用性,能够多个线程同一时候占用读模式的读写锁.可是仅仅能一个线程占用写模式的读写锁. 1. 当读写锁是写加锁状态时,在这个锁被解锁之前,全部试图对这个锁加锁的线程都会被堵塞 ...
- linux c编程:读写锁
什么是读写锁读写锁其实还是一种锁,是给一段临界区代码加锁,但是此加锁是在进行写操作的时候才会互斥,而在进行读的时候是可以共享的进行访问临界区的 为什么需要读写锁有时候,在多线程中,有一些公共数据修改的 ...
随机推荐
- 在flask中使用jsonify和json.dumps的区别
转载:https://blog.csdn.net/Duke_Huan_of_Qi/article/details/76064225
- docker和虚拟化技术的区别
1.docker和虚拟化技术的区别 Docker 扩展了 Linux 容器(Linux Containers),或着说 LXC,通过一个高层次的 API 为进程单独提供了一个轻量级的虚拟环境.Dock ...
- Oracle sql loader 使用案例
Listing 1: ---------------------- dir *.csv type abc.csv sqlplus scott/tiger@orcl create table emp1 ...
- Microsoft Visual C++ 14.0 is required问题的解决或warning: Debugger speedups using cython not found
今天在下载了 python3.64版本的安装后运行爬虫程序报错: warning: Debugger speedups using cython not found. Run '"C:\Py ...
- Git使用sublime_text作用默认编辑器
Git使用的是Vim来作用默认的编辑器,但一直都用不好这个编辑器,所以打算把他换成sublime_text 使用windows默认的记事本 git config --global core.edito ...
- Report Studio中目录结构报表浅析
一:场景:在一个报表中如果存在多个页面,每个页面显示不同的数据,如何通过目录控件实现对每一个报表的友好访问呢?下面我们就来看一下下面的效果,如下图1,2 图1:-------------------- ...
- [简谈]绕过HR破门而入的求职智慧
以往我们在网上看到的非常多求职文章或指导性纲领,譬如啥自信.做功课.良好形象.华丽的简历.工作经验.口才啥的,事实上到了21世纪尤其是互联网快速发展的今天,前面这些技巧就显得无比空洞: 1.由于自信谁 ...
- [JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)
We refactor a function that uses try/catch to a single composed expression using Either. We then int ...
- Android:Volley源代码解析
简单实例 Volley是一个封装HttpUrlConnection和HttpClient的网络通信框架,集AsyncHttpClient和Universal-Image-Loader的长处于了一身.既 ...
- Project has no project.properties file! Edit the project properties to set one.
解决办法: 右击项目,选择android tools-->fix project properties.然后重启eclipse即可.