Chinaunix

标题: 才学多线程读写锁,这个写线程为何一直取不到锁? [打印本页]

作者: qfmeal    时间: 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

作者: hanzhenlll    时间: 2016-02-04 15:15
适当的增加一些延迟 比如usleep(20) 之类。


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

作者: qfmeal    时间: 2016-02-05 15:28
我已经加了sleep(20)。   但是还跟原来一样。


其实我不让读线程1,2,处在循环中,程序运行就正常了。  就是不知道为什么。
作者: hellioncu    时间: 2016-02-05 15:39
读线程占用锁太频繁,你在加锁之后再sleep,写线程更难抢到锁了,放在读解锁之后才对。

另外读写锁可以设置写优先。
作者: lxyscls    时间: 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);
复制代码
设置“写优先”






欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2