Linux多线程实践(6) --Posix读写锁解决读者写者问题

来源:互联网 发布:网管软件平台 编辑:程序博客网 时间:2024/05/29 14:33

Posix读写锁

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,  
  2.                         const pthread_rwlockattr_t *restrict attr);  
  3. int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);  
  4.   
  5. int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);  
  6. int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);  
  7. int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);  

读写锁与互斥量类似, 不过读写锁允许更高的并行性. 读写锁用于读称为共享锁, 读写锁用于写称为排它锁;

读写锁规则:

  只要没有线程持有给定的读写锁用于写, 那么任意数目的线程可以持有读写锁用于读;

  仅当没有线程持有某个给定的读写锁用于读或用于写时, 才能分配读写锁用于写;

 

Posix自旋锁

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int pthread_spin_destroy(pthread_spinlock_t *lock);  
  2. int pthread_spin_init(pthread_spinlock_t *lock, int pshared);  
  3.   
  4. int pthread_spin_lock(pthread_spinlock_t *lock);  
  5. int pthread_spin_trylock(pthread_spinlock_t *lock);  
  6.   
  7. int pthread_spin_unlock(pthread_spinlock_t *lock);  

  自旋锁类似于互斥锁, 它的性能比互斥锁更高;

  自旋锁与互斥锁很重要的一个区别在于: 线程在申请自旋锁的时候, 线程并不会挂起, 它总是处于忙等待的状态(一直在自旋, CPU处于空耗的状态);

  自旋锁可用于以下情况:锁被持有的时间短, 而且线程并不希望在重新调度上花费太多的成本.

  自旋锁通常作为底层原语用于实现其他类型的锁: 比如有些互斥锁的实现在试图获取互斥量的时候会自旋一小段时间, 只有在自旋计数到达某一阈值的时候才会休眠; 因此, 很多互斥量的实现非常搞笑, 以至于应用程序采用互斥锁的性能与曾经采用过自旋锁的性能基本上是相同的.

  因此, 自旋锁只在某些特定的情况下有用, 比如在用户层, 自旋锁并不是非常有用, 除非运行在不允许抢占的实时调度类中.

 

读者写者问题

问题描述

  一个数据对象可以为多个并发进程所共享。其中有的进程可能只需要读共享对象的内容,而其他进程可能要更新共享对象的内容。

    读者:只对读感兴趣的进程;

    写者:其他进程(只写,或既读又写);

  规则

    允许多个读者同时读取数据;

    只有一个写者可以写数据;

    写者在写时读者不能读,反之亦然。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 实现1: 运用读写锁解决”读者写者问题” 
  2. 解题思路: 将需要读写的文件实现为一个字符串; 
  3. 读者进程: 一次可以将该字符串全部读出, 然后打印读取信息 
  4. 写者进程: 一次只能修改一个字符(该字符从A~Z循环写入), 修改之后打印写入信息 
  5. **/  
  6. //读写锁  
  7. pthread_rwlock_t rwlock;  
  8. const unsigned READERCOUNT = 2; //读者数  
  9. const unsigned WRITERCONUT = 5; //写者数  
  10.   
  11. const int PAPERSIZE = 32;       //文件长度  
  12. char paper[PAPERSIZE+1];        //文件  
  13.   
  14. unsigned short int write_index = 0; //写者需要写入的位置  
  15. char ch = 'A';  //写者需要写入的字母  
  16.   
  17. pthread_t thread[READERCOUNT+WRITERCONUT];  //读者+写者线程  
  18.   
  19. //读者线程  
  20. void *reader(void *args)  
  21. {  
  22.     int number = *(int *)args;  
  23.     delete (int *)args;  
  24.   
  25.     while (true)  
  26.     {  
  27.         //获取共享锁  
  28.         pthread_rwlock_rdlock(&rwlock);  
  29.         //开始读  
  30.         printf("## reader %d was reading...\n", number);  
  31.         printf("text: %s\n", paper);  
  32.         printf("   reader %d end reading...\n", number);  
  33.         //解锁共享锁  
  34.         pthread_rwlock_unlock(&rwlock);  
  35.   
  36.         sleep(1);  
  37.     }  
  38.     pthread_exit(NULL);  
  39. }  
  40. //写者线程  
  41. void *writer(void *args)  
  42. {  
  43.     int number = *(int *)args;  
  44.     delete (int *)args;  
  45.     while (true)  
  46.     {  
  47.         //获取写锁  
  48.         pthread_rwlock_wrlock(&rwlock);  
  49.         //开始写  
  50.         printf("++ writer %d was writing...\n", number);  
  51.         paper[write_index] = ch;  
  52.         write_index = (write_index+1)%PAPERSIZE;  
  53.         ch = ch+1;  
  54.         if (ch > 'Z')  
  55.             ch = 'A';  
  56.         printf("   writer %d end writing...\n", number);  
  57.         //释放写锁  
  58.         pthread_rwlock_unlock(&rwlock);  
  59.   
  60.         sleep(1);  
  61.     }  
  62.   
  63.     pthread_exit(NULL);  
  64. }  
  65.   
  66. int main()  
  67. {  
  68.     memset(paper, 0, sizeof(paper));  
  69.     pthread_rwlock_init(&rwlock, NULL);  
  70.   
  71.     for (unsigned int i = 0; i < READERCOUNT; ++i)  
  72.         pthread_create(&thread[i], NULL, reader, new int(i));  
  73.     for (unsigned int i = 0; i < WRITERCONUT; ++i)  
  74.         pthread_create(&thread[READERCOUNT+i], NULL, writer, new int(i));  
  75.     for (unsigned int i = 0; i < READERCOUNT+WRITERCONUT; ++i)  
  76.         pthread_join(thread[i], NULL);  
  77.   
  78.     pthread_rwlock_destroy(&rwlock);  
  79. }  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 实现2: 运用Posix信号量使用”读者优先”策略解决”读者写者问题” 
  2. 解题思路: 
  3. 如果新读者到: 
  4.    ①无读者、写者,新读者可以读; 
  5.    ②有写者等待,但有其它读者正在读,则新读者也可以读; 
  6.    ③有写者写,新读者等待。 
  7. 如果新写者到: 
  8.    ①无读者,新写者可以写; 
  9.    ②有读者,新写者等待; 
  10.    ③有其它写者,新写者等待。 
  11. **/  
  12. // 需要用两个互斥量实现  
  13. pthread_mutex_t rmutex;  
  14. pthread_mutex_t wmutex;  
  15.   
  16. const unsigned READERCOUNT = 5; //读者数  
  17. const unsigned WRITERCONUT = 5; //写者数  
  18.   
  19. const int PAPERSIZE = 32;       //文件长度  
  20. char paper[PAPERSIZE+1];        //文件  
  21.   
  22. unsigned short int write_index = 0; //写者需要写入的位置  
  23. char ch = 'A';  //写者需要写入的字母  
  24.   
  25. pthread_t thread[READERCOUNT+WRITERCONUT];  //读者+写者线程  
  26.   
  27. int nReader = 0;  
  28. //读者线程  
  29. void *reader(void *args)  
  30. {  
  31.     int number = *(int *)args;  
  32.     delete (int *)args;  
  33.   
  34.     while (true)  
  35.     {  
  36.         pthread_mutex_lock(&rmutex);  
  37.         //如果是第一个读者, 则锁定wmutex  
  38.         if (nReader == 0)  
  39.             pthread_mutex_lock(&wmutex);  
  40.         ++ nReader;  
  41.         pthread_mutex_unlock(&rmutex);  
  42.   
  43.         //开始读  
  44.         printf("## reader %d was reading...\n", number);  
  45.         printf("text: %s\n", paper);  
  46.         printf("   reader %d end reading...\n\n", number);  
  47.   
  48.         pthread_mutex_lock(&rmutex);  
  49.         -- nReader;  
  50.         //如果是最后一个读者, 则解锁wmutex  
  51.         if (nReader == 0)  
  52.             pthread_mutex_unlock(&wmutex);  
  53.         pthread_mutex_unlock(&rmutex);  
  54.   
  55.         sleep(1);  
  56.     }  
  57.     pthread_exit(NULL);  
  58. }  
  59.   
  60. //写者线程  
  61. void *writer(void *args)  
  62. {  
  63.     int number = *(int *)args;  
  64.     delete (int *)args;  
  65.     while (true)  
  66.     {  
  67.         //获取写锁  
  68.         pthread_mutex_lock(&wmutex);  
  69.         //开始写  
  70.         printf("++ writer %d was writing...\n", number);  
  71.         paper[write_index] = ch;  
  72.         write_index = (write_index+1)%PAPERSIZE;  
  73.         ch = ch+1;  
  74.         if (ch > 'Z')  
  75.             ch = 'A';  
  76.         printf("   writer %d end writing...\n\n", number);  
  77.         //释放写锁  
  78.         pthread_mutex_unlock(&wmutex);  
  79.   
  80.         sleep(1);  
  81.     }  
  82.   
  83.     pthread_exit(NULL);  
  84. }  
  85.   
  86. int main()  
  87. {  
  88.     memset(paper, 0, sizeof(paper));  
  89.     pthread_mutex_init(&rmutex, NULL);  
  90.     pthread_mutex_init(&wmutex, NULL);  
  91.   
  92.     for (unsigned int i = 0; i < READERCOUNT; ++i)  
  93.         pthread_create(&thread[i], NULL, reader, new int(i));  
  94.     for (unsigned int i = 0; i < WRITERCONUT; ++i)  
  95.         pthread_create(&thread[READERCOUNT+i], NULL, writer, new int(i));  
  96.     for (unsigned int i = 0; i < READERCOUNT+WRITERCONUT; ++i)  
  97.         pthread_join(thread[i], NULL);  
  98.   
  99.     pthread_mutex_destroy(&rmutex);  
  100.     pthread_mutex_destroy(&wmutex);  
  101. }  

  “读者优先”思想小结: 读者优先的设计思想是读进程只要看到有其它读进程正在读,就可以继续进行读;写进程必须等待所有读进程都不读时才能写,即使写进程可能比一些读进程更早提出申请。该算法只要还有一个读者在活动,就允许后续的读者进来,该策略的结果是,如果有一个稳定的读者流存在,那么这些读者将在到达后被允许进入。而写者就始终被挂起,直到没有读者为止.

0 0