Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4470102
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-05-16 17:03:56

读写锁:
    1. 如果某线程申请了读锁,其他线程可以再申请读锁,但不能申请写锁
    2. 如果某线程申请了写锁,其他线程不能申请 读锁 、 写锁


  1. 初始化: pthread_rwlock_init
  2. 阻塞读: pthread_rwlock_rdlock
  3. 非阻塞读: pthread_rwlock_tryrdlock
  4. 阻塞写: pthread_rwlock_wrlock
  5. 非阻塞写: pthread_rwlock_trywrlock
  6. 释放 : pthread_rwlock_unlock
  7. 销毁: pthread_rwlock_destroy
代码: pthread_rwlock.rar  

  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h> //exit
  4. #include <errno.h>
  5. #include <bits/pthreadtypes.h>

  6. static pthread_rwlock_t rwlock;
  7. #if 0
  8.  111: typedef struct _pthread_rwlock_t
  9.  112: {
  10.  113: struct _pthread_fastlock __rw_lock; /* Lock to guarantee mutual exclusion */
  11.  114: int __rw_readers; /* Number of readers */
  12.  115: _pthread_descr __rw_writer; /* Identity of writer, or NULL if none */
  13.  116: _pthread_descr __rw_read_waiting; /* Threads waiting for reading */
  14.  117: _pthread_descr __rw_write_waiting; /* Threads waiting for writing */
  15.  118: int __rw_kind; /* Reader/Writer preference selection */
  16.  119: int __rw_pshared; /* Shared between processes or not */
  17.  120: } pthread_rwlock_t;
  18. #endif

  19. #define WORK_SIZE 1024

  20. char work_area[WORK_SIZE]; // 共享全局数据
  21. int time_to_exit; //退出标识

  22. void print_res(int *a, char *s);

  23. void *thread_fun_read_o(void *a); //读线程 one
  24. void *thread_fun_read_t(void *a); //读线程 two
  25. void *thread_fun_write_o(void *a); //写线程 one
  26. void *thread_fun_write_t(void *a); //写线程 two

  27. int main(int argc, char *argv[])
  28. {
  29.     int res;
  30.     pthread_t a_thread,b_thread,c_thread,d_thread; //unsigned long

  31.     void *thread_result; //thread_join 参数

  32.     //初始化 读写锁
  33.     res = pthread_rwlock_init(&rwlock, NULL);
  34.     print_res(&res, "thread_rwlock");

  35.     //产生 4 个子线程
  36.     res = pthread_create(&a_thread, NULL, thread_fun_read_o, NULL);
  37.     print_res(&res,"pthread_create a ");

  38.     res = pthread_create(&b_thread, NULL, thread_fun_read_t, NULL);
  39.     print_res(&res,"pthread_create b ");

  40.     res = pthread_create(&c_thread, NULL, thread_fun_write_o, NULL);
  41.     print_res(&res,"pthread_create c ");

  42.     res = pthread_create(&d_thread, NULL, thread_fun_write_t, NULL);
  43.     print_res(&res,"pthread_create d ");
  44.     
  45.     // 等待 4 个子线程 退出
  46.     res = pthread_join(a_thread, &thread_result);
  47.     print_res(&res, "pthread_join a");

  48.     res = pthread_join(b_thread, &thread_result);
  49.     print_res(&res, "pthread_join b");

  50.     res = pthread_join(c_thread, &thread_result);
  51.     print_res(&res, "pthread_join c");

  52.     res = pthread_join(d_thread, &thread_result);
  53.     print_res(&res, "pthread_join d");

  54.     pthread_rwlock_destroy(&rwlock);

  55.     exit(EXIT_SUCCESS);

  56. }

  57. void print_res(int *a, char *s)
  58. {
  59.     if(*a != 0)
  60.     {
  61.         printf("%s failed.\n",s);
  62.         exit(EXIT_FAILURE);
  63.     }
  64.     else
  65.     {
  66.         printf("%s success\n",s);
  67.     }
  68. }

  69. void *thread_fun_read_o(void *a)
  70. {
  71.     printf("thread read one try to get lock\n");
  72.     pthread_rwlock_rdlock(&rwlock); //获取 读取锁
  73.     while(strncmp("end", work_area, 3) != 0)
  74.     {
  75.         printf("this is thread read one.");
  76.         printf("the characters is %s\n",work_area); //输出
  77.         
  78.         pthread_rwlock_unlock(&rwlock); //解锁
  79.         sleep(2);
  80.     
  81.         pthread_rwlock_rdlock(&rwlock); //获取 读取锁

  82.         while(work_area[0] == '\0')
  83.         {
  84.             pthread_rwlock_unlock(&rwlock);
  85.             sleep(2);
  86.             pthread_rwlock_rdlock(&rwlock);
  87.         }
  88.     }
  89.     pthread_rwlock_unlock(&rwlock);
  90.     time_to_exit = 1;
  91.     pthread_exit(0);
  92. }

  93. void *thread_fun_read_t(void *a)
  94. {
  95.     printf("thread read two try to get lock");
  96.     pthread_rwlock_rdlock(&rwlock); //获取 读取锁
  97.     while(strncmp("end", work_area, 3) != 0)
  98.     {
  99.         printf("this is thread read two.\n");
  100.         printf("the characters is %s\n",work_area); //输出
  101.         
  102.         pthread_rwlock_unlock(&rwlock); //解锁
  103.         sleep(5);
  104.     
  105.         pthread_rwlock_rdlock(&rwlock); //获取 读取锁

  106.         while(work_area[0] == '\0')
  107.         {
  108.             pthread_rwlock_unlock(&rwlock);
  109.             sleep(5);
  110.             pthread_rwlock_rdlock(&rwlock);
  111.         }
  112.     }
  113.     pthread_rwlock_unlock(&rwlock);
  114.     time_to_exit = 1;
  115.     pthread_exit(0);
  116. }

  117. void *thread_fun_write_o(void *a)
  118. {
  119.     printf("this is write thread one try to get lock\n");
  120.     while(!time_to_exit)
  121.     {
  122.         pthread_rwlock_wrlock(&rwlock);
  123.         printf("this is write thread one.input some text. enter 'end' to finish\n");
  124.         fgets(work_area, WORK_SIZE, stdin);
  125.         pthread_rwlock_unlock(&rwlock);
  126.         sleep(15);
  127.     }
  128.     pthread_rwlock_unlock(&rwlock);
  129.     pthread_exit(0);
  130. }

  131. void *thread_fun_write_t(void *a)
  132. {
  133.     sleep(10);
  134. //    printf("this is write thread two try to get lock\n");
  135.     while(!time_to_exit)
  136.     {
  137.         pthread_rwlock_wrlock(&rwlock);
  138.         printf("this is write thread two.input some text. enter 'end' to finish\n");
  139.         fgets(work_area, WORK_SIZE, stdin);
  140.         pthread_rwlock_unlock(&rwlock);
  141.         sleep(20);
  142.     }
  143.     pthread_rwlock_unlock(&rwlock);
  144.     pthread_exit(0);
  145. }

  1. ywx@yuweixian:~/yu/professional/4$ ./pthread_rwlock thread_rwlock success
  2. pthread_create a success
  3. pthread_create b success
  4. pthread_create c success
  5. thread read one try to get lock
  6. this is thread read one.the characters is
  7. pthread_create d success
  8. thread read two try to get lockthis is thread read two.
  9. the characters is
  10. this is write thread one try to get lock
  11. this is write thread one.input some text. enter 'end' to finish
  12. abcdefghijklmnopqrstuvwxyz
  13. this is write thread two.input some text. enter 'end' to finish
  14. 123456789
  15. this is thread read one.the characters is 123456789

  16. this is thread read two.
  17. the characters is 123456789

  18. this is thread read one.the characters is 123456789

  19. this is thread read one.the characters is 123456789

  20. this is write thread one.input some text. enter 'end' to finish
  21. end
  22. pthread_join a success
  23. pthread_join b success
  24. pthread_join c success
  25. pthread_join d success

















阅读(1804) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~