读者写者问题
2016-04-24 22:43
330 查看
前言:上面一篇,我们介绍了互斥锁,这儿的读者写者问题需要用到自旋锁。
为什么会有自旋锁
在编写多线程的时候,有些公共数据读的概率远远大于修改的几率。通常而言,在读的过程中,往往伴随着查找的操作,中间耗时很长。给这种代码段加锁,会极大地降低我们程序的效率。我们引入了读写锁即自旋锁处理这种多读少写的情况。
2.什么是自旋锁
(1)它把对共享资源的访问者划分成读者和写者,读者只对共享资源进行读访问,写者则需要对共享资源进行写操作。
(2)这种锁相对于自旋锁而言,能提高并发性。
(3)在多处理器系统中:
a.对于读者:它允许同时有多个读者来访问共享资源,最大可能的读者数为实际的逻辑CPU数。
b.对于写者:写者是排他性的,一个读写锁同时只能有一个写者或多个读者(与CPU数相关),但不能同时既有读者又有写者。
3.相关函数
(基于写者)
(基于读者)
成功返回0,失败返回错误码。
4.代码实现
5.结果分析:
由输出的结果知道,read中的
为什么会有自旋锁
在编写多线程的时候,有些公共数据读的概率远远大于修改的几率。通常而言,在读的过程中,往往伴随着查找的操作,中间耗时很长。给这种代码段加锁,会极大地降低我们程序的效率。我们引入了读写锁即自旋锁处理这种多读少写的情况。
2.什么是自旋锁
(1)它把对共享资源的访问者划分成读者和写者,读者只对共享资源进行读访问,写者则需要对共享资源进行写操作。
(2)这种锁相对于自旋锁而言,能提高并发性。
(3)在多处理器系统中:
a.对于读者:它允许同时有多个读者来访问共享资源,最大可能的读者数为实际的逻辑CPU数。
b.对于写者:写者是排他性的,一个读写锁同时只能有一个写者或多个读者(与CPU数相关),但不能同时既有读者又有写者。
3.相关函数
(基于写者)
(基于读者)
成功返回0,失败返回错误码。
4.代码实现
//test.c 1 #include<stdio.h> 2 #include<pthread.h> 3 4 pthread_rwlock_t lock; 5 int g_val=0; 6 7 void* read(void* arg) 8 { 9 while(1) 10 { 11 if(pthread_rwlock_rdlock(&lock)!=0) 12 { 13 printf("writer is writing,reader waiting...\n"); 14 } 15 else 16 { 17 printf("reader%d read val is:%d\n",(int)arg,g_val); 18 pthread_rwlock_unlock(&lock); 19 } 20 sleep(1); 21 } 22 } 23 void* write(void* arg) 24 { 25 while(1) 26 { 27 if(pthread_rwlock_trywrlock(&lock)!=0) 28 { 29 printf("reader is reading,writer waiting...\n"); 30 sleep(1); 31 } 32 else 33 { 34 g_val++; 35 printf("writer%d write val is:%d\n",(int)arg,g_val); 36 pthread_rwlock_unlock(&lock); 37 } 38 sleep(1); 39 } 40 } 41 int main() 42 { 43 pthread_rwlock_init(&lock,NULL); 44 pthread_t writer1; 45 pthread_t writer2; 46 47 pthread_t reader1; 48 pthread_t reader2; 49 pthread_t reader3; 50 51 pthread_create(&writer1,NULL,write,(void*)1); 52 pthread_create(&writer2,NULL,write,(void*)2); 53 pthread_create(&reader1,NULL,read,(void*)1); 54 pthread_create(&reader2,NULL,read,(void*)2); 55 pthread_create(&reader3,NULL,read,(void*)3); 56 57 pthread_join(writer1,NULL); 58 pthread_join(writer2,NULL); 59 pthread_join(reader1,NULL); 60 pthread_join(reader2,NULL); 61 pthread_join(reader3,NULL); 62 63 pthread_rwlock_destroy(&lock); 64 65 return 0; 66 } //makefile 1 test:test.c 2 gcc -o $@ $^ -lpthread 3 .PHONY:clean 4 clean: 5 rm -f test输出结果:
5.结果分析:
由输出的结果知道,read中的
printf("writer is writing,reader waiting...\n");和write中的
printf("reader is reading,writer waiting...\n");从未输出过知道,自旋锁设定为写的优先级高于读
相关文章推荐
- Python3写爬虫(四)多线程实现数据爬取
- C#实现多线程的同步方法实例分析
- 浅谈chuck-lua中的多线程
- C#简单多线程同步和优先权用法实例
- C#多线程学习之(四)使用线程池进行多线程的自动管理
- C#多线程编程中的锁系统(三)
- 解析C#多线程编程中异步多线程的实现及线程池的使用
- C#多线程学习之(六)互斥对象用法实例
- 基于一个应用程序多线程误用的分析详解
- C#多线程学习之(三)生产者和消费者用法分析
- C#多线程学习之(一)多线程的相关概念分析
- C#多线程之Thread中Thread.IsAlive属性用法分析
- 分享我在工作中遇到的多线程下导致RCW无法释放的问题
- C#多线程编程之使用ReaderWriterLock类实现多用户读与单用户写同步的方法
- C#控制台下测试多线程的方法
- 笔记本下什么是迅驰处理器(cpu)相关资料第1/2页
- 21天学习android开发教程之SurfaceView与多线程的混搭
- Ruby 多线程的潜力和弱点分析
- C#中WPF使用多线程调用窗体组件的方法
- C#如何对多线程、多任务管理(demo)