概述
读写锁与互斥量类似,不过读写锁允许更高的并行性。互斥量要么是锁住状态,要么是不加锁状态,而且一次只有一个线程对其加锁。读写锁可以有三种状态:读模式下加锁状态,写模式下加锁状态,不加锁状态。一次只有一个线程可以占有写模式的读写锁,但是多个线程可用同时占有读模式的读写锁。读写锁也叫做共享-独占锁,当读写锁以读模式锁住时,它是以共享模式锁住的,当它以写模式锁住时,它是以独占模式锁住的。
在对数据的读写应用中,更多的是读操作,而写操作较少,例如对数据库数据的读写应用。为了满足当前能够允许多个读出,但只允许一个写入的需求,线程提供了读写锁来实现。其基本原则如下:
(1)如果有其它线程读数据,则允许其它线程执行读操作,但不允许写操作;(2)如果有其它线程写数据,则其它线程的读、写操作均允许。
因此,其将该锁分为了读锁和写锁。
(1)如果某线程申请了读锁,其它线程可以再申请读锁,但不能申请写锁;
(2)如果某线程申请了写锁,则其它线程不能申请读锁,也不能申请写锁。
定义读写锁对象的代码如下:
- pthread_rwlock_t rwlock; //全局变量
读写锁API
读写锁的数据类型为pthread_rwlock_t。如果这个类型的某个变量是静态分配的,那么可通过给它赋常值PTHREAD_RWLOCK_INITIALIZER来初始化它。
阻塞获取读写锁:
- int pthread_rwlock_rdlock(pthread_rwlock_t *rwptr); //获取一个读出锁
- int pthread_rwlock_wrlock(pthread_rwlock_t *rwptr); //获取一个写入锁
非阻塞获取读写锁:
- int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwptr);
- int pthread_rwlock_trywrlock(pthread_rwlock_t *rwptr);
解锁:
- int pthread_rwlock_unlock(pthread_rwlock_t *rwptr); //释放一个写入锁或者读出锁
- int pthread_rwlock_init(pthread_rwlock_t *rwptr, const pthread_rwlockattr_t *attr)
- int pthread_rwlock_destroy(pthread_rwlock_t *rwptr);
- int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
- int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
- int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, int *valptr);
- int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int valptr);
读写锁应用实例
- #include <errno.h>
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <bits/pthreadtypes.h>
- static pthread_rwlock_t rwlock;
- #define WORK_SIZE 1024
- char work_area[WORK_SIZE];
- int time_to_exit;
- void *thread_function_read_o(void *arg);
- void *thread_function_read_t(void *arg);
- void *thread_function_write_o(void *arg);
- void *thread_function_write_t(void *arg);
- int main(int argc,char *argv[])
- {
- int res;
- pthread_t a_thread,b_thread,c_thread,d_thread;
- void *thread_result;
- res=pthread_rwlock_init(&rwlock,NULL);
- if (res != 0)
- {
- perror("rwlock initialization failed");
- exit(EXIT_FAILURE);
- }
- res = pthread_create(&a_thread, NULL, thread_function_read_o, NULL);//create new thread
- if (res != 0)
- {
- perror("Thread creation failed");
- exit(EXIT_FAILURE);
- }
- res = pthread_create(&b_thread, NULL, thread_function_read_t, NULL);//create new thread
- if (res != 0)
- {
- perror("Thread creation failed");
- exit(EXIT_FAILURE);
- }
- res = pthread_create(&c_thread, NULL, thread_function_write_o, NULL);//create new thread
- if (res != 0)
- {
- perror("Thread creation failed");
- exit(EXIT_FAILURE);
- }
- res = pthread_create(&d_thread, NULL, thread_function_write_t, NULL);//create new thread
- if (res != 0)
- {
- perror("Thread creation failed");
- exit(EXIT_FAILURE);
- }
- res = pthread_join(a_thread, &thread_result);
- if (res != 0)
- {
- perror("Thread join failed");
- exit(EXIT_FAILURE);
- }
- res = pthread_join(b_thread, &thread_result);
- if (res != 0)
- {
- perror("Thread join failed");
- exit(EXIT_FAILURE);
- }
- res = pthread_join(c_thread, &thread_result);
- if (res != 0)
- {
- perror("Thread join failed");
- exit(EXIT_FAILURE);
- }
- res = pthread_join(d_thread, &thread_result);
- if (res != 0)
- {
- perror("Thread join failed");
- exit(EXIT_FAILURE);
- }
- pthread_rwlock_destroy(&rwlock);
- exit(EXIT_SUCCESS);
- }
- void *thread_function_read_o(void *arg)
- {
- printf("thread read one try to get lock\n");
- pthread_rwlock_rdlock(&rwlock);
- while(strncmp("end", work_area, 3) != 0)
- {
- printf("this is thread read one.");
- printf("the characters is %s",work_area);
- pthread_rwlock_unlock(&rwlock);
- sleep(2);
- pthread_rwlock_rdlock(&rwlock);
- while (work_area[0] == '\0' )
- {
- pthread_rwlock_unlock(&rwlock);
- sleep(2);
- pthread_rwlock_rdlock(&rwlock);
- }
- }
- pthread_rwlock_unlock(&rwlock);
- time_to_exit=1;
- pthread_exit(0);
- }
- void *thread_function_read_t(void *arg)
- {
- printf("thread read one try to get lock\n");
- pthread_rwlock_rdlock(&rwlock);
- while(strncmp("end", work_area, 3) != 0)
- {
- printf("this is thread read two.");
- printf("the characters is %s",work_area);
- pthread_rwlock_unlock(&rwlock);
- sleep(5);
- pthread_rwlock_rdlock(&rwlock);
- while (work_area[0] == '\0' )
- {
- pthread_rwlock_unlock(&rwlock);
- sleep(5);
- pthread_rwlock_rdlock(&rwlock);
- }
- }
- pthread_rwlock_unlock(&rwlock);
- time_to_exit=1;
- pthread_exit(0);
- }
- void *thread_function_write_o(void *arg)
- {
- printf("this is write thread one try to get lock\n");
- while(!time_to_exit)
- {
- pthread_rwlock_wrlock(&rwlock);
- printf("this is write thread one.\nInput some text. Enter 'end' to finish\n");
- fgets(work_area, WORK_SIZE, stdin);
- pthread_rwlock_unlock(&rwlock);
- sleep(15);
- }
- pthread_rwlock_unlock(&rwlock);
- pthread_exit(0);
- }
- void *thread_function_write_t(void *arg)
- {
- sleep(10);
- while(!time_to_exit)
- {
- pthread_rwlock_wrlock(&rwlock);
- printf("this is write thread two.\nInput some text. Enter 'end' to finish\n");
- fgets(work_area, WORK_SIZE, stdin);
- pthread_rwlock_unlock(&rwlock);
- sleep(20);
- }
- pthread_rwlock_unlock(&rwlock);
- pthread_exit(0);
- }
- $ ./pthread_rwlock_example
- this is write thread one try to get lock
- thread read one try to get lock
- this is write thread one.
- Input some text. Enter 'end' to finish
- thread read one try to get lock
- wr one test
- this is thread read two.the characters is wr one test
- this is thread read one.the characters is wr one test
- this is thread read one.the characters is wr one test
- this is thread read one.the characters is wr one test
- this is thread read two.the characters is wr one test
- this is write thread two.
- Input some text. Enter 'end' to finish
- wr two test
- this is thread read one.the characters is wr two test
- this is thread read two.the characters is wr two test
- this is thread read one.the characters is wr two test
- this is write thread one.
- Input some text. Enter 'end' to finish
- end