linux多线程之读写锁

基本概念:

读写锁也叫做共享互斥锁。

当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞。当读写锁在读加锁状态,所有试图以读模式对它进行加锁的线程都可以得到访问权。

与互斥量相比,读写锁在使用之前必须初始化,在释放它们底层的内存之前必须销毁。


一、锁的初始化与销毁

  1. PTHREAD_RWLOCK_DESTROY(P) POSIX Programmer' s Manual PTHREAD_RWLOCK_DESTROY(P)
  2. NAME
  3. pthread_rwlock_destroy, pthread_rwlock_init - destroy and initialize a
  4. read-write lock object
  5. SYNOPSIS
  6. #include <pthread.h>
  7. int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
  8. int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
  9. const pthread_rwlockattr_t * restrict attr);
两个函数的返回值:若成功,返回0;否则,返回错误编号


二、读锁

  1. PTHREAD_RWLOCK_RDLOCK(P) POSIX Programmer' s Manual PTHREAD_RWLOCK_RDLOCK(P)
  2. NAME
  3. pthread_rwlock_rdlock, pthread_rwlock_tryrdlock - lock a read-write
  4. lock object for reading
  5. SYNOPSIS
  6. #include <pthread.h>
  7. int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
  8. int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
两个函数的返回值:若成功,返回0;否则,返回错误编号
pthread_rwlock_tryrdlock函数可以获取锁时,返回0.否则,返回错误EBUSY


三、写锁

  1. PTHREAD_RWLOCK_TRYWRLOCK(P)POSIX Programmer' s ManuaPTHREAD_RWLOCK_TRYWRLOCK(P)
  2. NAME
  3. pthread_rwlock_trywrlock, pthread_rwlock_wrlock - lock a read-write
  4. lock object for writing
  5. SYNOPSIS
  6. #include <pthread.h>
  7. int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
  8. int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
两个函数的返回值:若成功,返回0;否则,返回错误编号

pthread_rwlock_trywrlock函数可以获取锁时,返回0.否则,返回错误EBUSY


四、带有超时的读写锁

  1. PTHREAD_RWLOCK_TIMEDWRLOCK(POSIX Programmer's ManPTHREAD_RWLOCK_TIMEDWRLOCK(P)
  2. NAME
  3. pthread_rwlock_timedrdlock - lock a read-write lock for reading
  4. pthread_rwlock_timedwrlock - lock a read-write lock for writing
  5. SYNOPSIS
  6. #include <pthread.h>
  7. #include <time.h>
  8. int pthread_rwlock_timedrdlock( pthread_rwlock_t * restrict rwlock,
  9. const struct timespec * restrict abs_timeout);
  10. int pthread_rwlock_timedwrlock( pthread_rwlock_t * restrict rwlock,
  11. const struct timespec * restrict abs_timeout);
两个函数的返回值:若成功,返回0;否则,返回错误编号
如果它们不能获取锁,那么超时到期时,这两个函数将返回ETIMEDOUT


五、解锁

  1. PTHREAD_RWLOCK_UNLOCK(P) POSIX Programmer' s Manual PTHREAD_RWLOCK_UNLOCK(P)
  2. NAME
  3. pthread_rwlock_unlock - unlock a read-write lock object
  4. SYNOPSIS
  5. #include <pthread.h>
  6. int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
返回值:若成功,返回0;否则,返回错误编号

读写锁属性:

读写锁支持的唯一属性是进程共享属性。它与互斥量的进程共享属性是相同的,这里不展开讨论。


例子:gcc pthread_rwlock.c -pthread

  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. static int num = 0;
  7. static int count = 100000;
  8. static pthread_rwlock_t rwlock;
  9. void Perror(const char *s)
  10. {
  11. perror(s);
  12. exit(EXIT_FAILURE);
  13. }
  14. void* fun2(void *arg)
  15. {
  16. pthread_t thread_id = pthread_self();
  17. printf( "the thread2 id is %ld\n", ( long)thread_id);
  18. int i = 1;
  19. for (; i<=count; ++i) {
  20. pthread_rwlock_wrlock(&rwlock);
  21. num += 1;
  22. pthread_rwlock_unlock(&rwlock);
  23. }
  24. }
  25. void* fun3(void *arg)
  26. {
  27. pthread_t thread_id = pthread_self();
  28. printf( "the thread3 id is %ld\n", ( long)thread_id);
  29. int i = 1;
  30. for (; i<=count; ++i) {
  31. pthread_rwlock_wrlock(&rwlock);
  32. num += 1;
  33. pthread_rwlock_unlock(&rwlock);
  34. }
  35. }
  36. int main()
  37. {
  38. int err;
  39. pthread_t thread1;
  40. pthread_t thread2;
  41. pthread_t thread3;
  42. // init
  43. pthread_rwlock_init(&rwlock, NULL);
  44. thread1 = pthread_self();
  45. printf( "the thread1 id is %ld\n", ( long)thread1);
  46. // Create thread
  47. err = pthread_create(&thread2, NULL, fun2, NULL);
  48. if (err != 0) {
  49. Perror( "can't create thread2\n");
  50. }
  51. err = pthread_create(&thread3, NULL, fun3, NULL);
  52. if (err != 0) {
  53. Perror( "can't create thread3\n");
  54. }
  55. // detach thread3
  56. err = pthread_detach(thread2);
  57. if (err != 0) {
  58. Perror( "can't detach thread2\n");
  59. }
  60. err = pthread_detach(thread3);
  61. if (err != 0) {
  62. Perror( "can't detach thread3\n");
  63. }
  64. int i = 1;
  65. for (; i<=count; ++i) {
  66. pthread_rwlock_rdlock(&rwlock);
  67. int temp = num;
  68. pthread_rwlock_unlock(&rwlock);
  69. }
  70. sleep( 10);
  71. printf( "The num is %d\n", num);
  72. pthread_rwlock_destroy(&rwlock);
  73. return 0;
  74. }


参考:《unix环境高级编程》·第三版

End;

猜你喜欢

转载自blog.csdn.net/qingzhuyuxian/article/details/81012054