免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2747 | 回复: 4
打印 上一主题 下一主题

[C++] 才学多线程读写锁,这个写线程为何一直取不到锁? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-02-04 14:34 |只看该作者 |倒序浏览
才学习多线程读写锁。
参照网上的教程,自己写了简单的例子,但是写线程一直都未进入,高手帮忙看一下我哪里错了,谢谢!
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include <iostream>

  5. using namespace std;


  6. int g_count = 0;
  7. pthread_rwlock_t rwlock;

  8. void * thd_func_read_1(void * arg)
  9. {
  10.     while(1)
  11.     {
  12.         pthread_rwlock_rdlock(&rwlock);
  13.         sleep(1);
  14.         cout<<"read1 count:"<<g_count<<endl;
  15.         pthread_rwlock_unlock(&rwlock);
  16.     }
  17.     return NULL;
  18. }

  19. void * thd_func_read_2(void * arg)
  20. {
  21.     while(1)
  22.     {
  23.         pthread_rwlock_rdlock(&rwlock);
  24.         sleep(1);
  25.         cout<<"read2 count:"<<g_count<<endl;
  26.         pthread_rwlock_unlock(&rwlock);
  27.     }
  28.     return NULL;
  29. }

  30. void * thd_func_write(void * arg)
  31. {
  32.     while(1)
  33.     {
  34.         pthread_rwlock_wrlock(&rwlock);
  35.         sleep(1);
  36.         cout<<"write count:"<<++g_count<<endl;
  37.         if( g_count >= 5 )
  38.         {
  39.             pthread_rwlock_unlock(&rwlock);
  40.             pthread_exit(0);
  41.             return NULL;
  42.         }
  43.         pthread_rwlock_unlock(&rwlock);
  44.     }
  45.     return NULL;
  46. }

  47. int main(int argc, char **argv)
  48. {
  49.     int ret;
  50.     pthread_t thd_read_1, thd_read_2, thd_write;
  51.     pthread_rwlock_init(&rwlock, NULL);

  52.     ret = pthread_create(&thd_read_1, NULL, thd_func_read_1, NULL); //创建线程1
  53.     if(ret){
  54.         perror("thd_read_1:\n");
  55.         return 1;
  56.     }

  57.     ret = pthread_create(&thd_read_2, NULL, thd_func_read_2, NULL); //创建线程2
  58.     if(ret){
  59.         perror("thd_read_2: \n");
  60.         return 1;
  61.     }

  62.     ret = pthread_create(&thd_write, NULL, thd_func_write, NULL); //创建线程2
  63.     if(ret){
  64.         perror("thd_read_3: \n");
  65.         return 1;
  66.     }

  67.     pthread_join(thd_read_1,NULL);
  68.     pthread_join(thd_read_2,NULL);
  69.     pthread_join(thd_write,NULL);

  70.     return 0;
  71. }
复制代码
输出的内容也很奇怪:
read2 count:read1 count:00

read1 count:read2 count:00

read1 count:read2 count:00

read1 count:0
read2 count:0
read2 count:0
read1 count:0
read1 count:read2 count:00

read1 count:read2 count:00

read1 count:0
read2 count:0
read2 count:read1 count:00

论坛徽章:
1
射手座
日期:2014-08-04 16:49:43
2 [报告]
发表于 2016-02-04 15:15 |只看该作者
适当的增加一些延迟 比如usleep(20) 之类。


  过度的快速循环使用锁 本身就是一种不健康的设计。

论坛徽章:
0
3 [报告]
发表于 2016-02-05 15:28 |只看该作者
我已经加了sleep(20)。   但是还跟原来一样。


其实我不让读线程1,2,处在循环中,程序运行就正常了。  就是不知道为什么。

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
4 [报告]
发表于 2016-02-05 15:39 |只看该作者
读线程占用锁太频繁,你在加锁之后再sleep,写线程更难抢到锁了,放在读解锁之后才对。

另外读写锁可以设置写优先。

论坛徽章:
14
水瓶座
日期:2014-06-10 09:51:0215-16赛季CBA联赛之江苏
日期:2017-11-27 11:42:3515-16赛季CBA联赛之八一
日期:2017-04-12 14:26:2815-16赛季CBA联赛之吉林
日期:2016-08-20 10:43:1215-16赛季CBA联赛之广夏
日期:2016-06-23 09:53:58程序设计版块每日发帖之星
日期:2016-02-11 06:20:00程序设计版块每日发帖之星
日期:2016-02-09 06:20:0015-16赛季CBA联赛之上海
日期:2015-12-25 16:40:3515-16赛季CBA联赛之广夏
日期:2015-12-22 09:39:36程序设计版块每日发帖之星
日期:2015-08-24 06:20:002015亚冠之德黑兰石油
日期:2015-08-07 09:57:302015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2016-02-15 14:47 |只看该作者
本帖最后由 lxyscls 于 2016-02-15 14:47 编辑

回复 1# qfmeal


    [root@router ~]# ./test
read1 count:read2 count:00

write count:1
write count:2
write count:3
write count:4
write count:5
read1 count:read2 count:5
5
read1 count:5
read2 count:5
  1.     pthread_rwlockattr_t attr;
  2.     pthread_rwlockattr_init(&attr);
  3.     pthread_rwlockattr_setkind_np(&attr, 2);
  4.     pthread_rwlock_init(&rwlock, &attr);
复制代码
设置“写优先”

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP