一、读写锁基本原理
读写锁与互斥锁类似,不过读写锁运行更高的并行。互斥量要么锁住状态,要么就是不加锁状态,而且一次只有一个线程可以对其加锁。读写锁可以有3中状态:读模式下加锁状态,写模式下加锁状态,不加锁状态。一次只有一个线程可以占有写模式的读写锁,但是多个线程可以同时占有读模式的读写锁。
(1)当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的进程都会被阻塞。
(2)当读写锁是读加锁状态时,所有试图以读模式对它进行加锁的线程都可以得到访问权,但是任何希望以写模式对此锁进行加锁的线程都会阻塞,直到所有的线程释放它们的写锁为止。但是当读写锁处于读模式锁住状态时,有一个线程试图以写模式获取锁时,读写锁通常会阻塞随后的读模式锁请求。这样可以避免读模式锁长期占有,而等待的写模式锁请求一直得不到满足。
读写锁非常适合对数据结构的读次数远大于写的情况。当读写锁在写模式下时,它所保护的数据结构就可以被完全地修改,因为一次只有一个线程可以再写模式下拥有这个锁。当读写锁在读模式下时,只要线程先获取了读模式下的读写锁,该锁所抱回的数据结构就可以被多个获得读模式锁的线程读取。
读写锁也叫做共享互斥锁。当读写锁是读模式锁住时,就可以说成是以共享模式锁住。当它是写模式锁住的时候,就可以说成是以互斥模式锁住的。
定义读写锁对象:
<span style="font-family:Microsoft YaHei;font-size:18px;">pthread_rwlock_t rwlock; //全局变量</span>
二、读写锁基本操作
1、初始化/损坏读写锁
<span style="font-family:Microsoft YaHei;font-size:18px;">#include <pthread.h>
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_destory(pthread_rwlock_t *rwlock);
//两个函数的返回值:若成功,返回0;否则,返回错误编号</span>
第一个参数rwlock是指向要初始化的读写锁的指针。
第二个参数attr指向属性对象的指针,该属性对象定义要初始化的读写锁的特性,此参数如果设置为NULL,则使用默认属性。
可以使用宏PTHREAD_RWLOCK_INITIALIZER初始化静态分配的读写锁。这种效果相当于使用NULL指定的attr参数调用pthread_rwlock_init()完成的动态初始化,区别在于,它不执行错误检查,将使用默认的属性初始化读写锁。
2、申请读锁
<span style="font-family:Microsoft YaHei;font-size:18px;">#include <pthread.h>
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
//两个函数返回值:若成功,返回0;否则,返回错误编号</span>
pthread_rwlock_rdlock()函数以阻塞的方式来申请读锁。
pthread_rwlock_tryrdlock()函数以非阻塞的方式来申请读锁。
如果不能申请到该读锁,pthread_rwlock_rdlock将阻塞当前进程,而pthread_rwlock_tryrdlock将返回错误。
3、申请写锁
<span style="font-family:Microsoft YaHei;font-size:18px;">#include <pthread.h>
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
//两个函数返回值:若成功,返回0;否则,返回错误编号</span>
pthread_rwlock_wrlock()函数以阻塞的方式来申请写锁。
pthread_rwlock_trywrlock()函数以非阻塞的方式来申请写锁。
如果不能申请到该写锁,pthread_rwlock_wrlock将阻塞当前进程,而pthread_rwlock_trywrlock将返回错误。
4、解锁
无论是读锁还是写锁都将使用函数pthread_rwlock_unlock()来解锁。
<span style="font-family:Microsoft YaHei;font-size:18px;">#include <pthread.h>
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
//返回值:若成功,返回0;否则,返回错误编号</span>
(1)如果调用此函数来释放读写锁锁定rwlock上的读锁定,但当前在此读写锁上还保持着其他读锁定,则读写锁将保持线程读锁定状态,只不过当前进程已不再是其所有者之一。
(2)如果此函数释放读写锁的最后一个读锁,则对象将处于没有所有者的解锁状态。
(3)如果调用此函数释放读写锁的写锁,则读写锁定将处于没有所有者的解锁状态。
三、读写锁应用实例
1、程序说明
下面是一个使用读写锁来实现4个线程读写一段数据的实例。在此示例程序中,共创建了4个新的线程,其中两个线程用来读数据,两个线程用来写入数据。从运行结果可以看出,在任意时刻,如果有一个线程写数据(使用写锁定),将阻塞所有其他线程任何操作,但在某一时刻,如果有一个线程读数据(使用读写锁的读锁定),则其他线程任然可以获得读数据锁。即可以执行读操作。
2、源码分析
<span style="font-family:Microsoft YaHei;font-size:18px;">#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
static pthread_rwlock_t rwlock; //读写锁对象
#define WORK_SIZE 1024
char work_area[WORK_SIZE]; //共享全局数据
int time_to_exit = 0; //退出标识
void *thread_function_read_o(void *arg); //读线程one
void *thread_function_read_t(void *arg); //读线程two
void *thread_function_write_o(void *arg); //写线程one
void *thread_function_write_t(void *arg); //写线程two
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); //创建线程
if (res != 0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
res = pthread_create(&b_thread, NULL, thread_function_read_t, NULL); //创建线程
if (res != 0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
res = pthread_create(&c_thread, NULL, thread_function_write_o, NULL); //创建线程
if (res != 0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
res = pthread_create(&d_thread, NULL, thread_function_write_t, NULL); //创建线程
if (res != 0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
res = pthread_join(a_thread, &thread_result); //等待线程a结束
if (res != 0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
res = pthread_join(b_thread, &thread_result); //等待线程b结束
if (res != 0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
res = pthread_join(c_thread, &thread_result); //等待线程c结束
if (res != 0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
res = pthread_join(d_thread, &thread_result); //等待线程d结束
if (res != 0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
pthread_rwlock_destroy(&rwlock); //销毁读写锁
exit(EXIT_FAILURE);
}
void *thread_function_read_o(void *arg) //读线程one
{
printf("thread read one try to get lock\n");
pthread_rwlock_rdlock(&rwlock); //获取读取锁
while(strncmp("end", work_area, 3) != 3) //比较是否为结束符号
{
printf("this is thread read one.");
printf("the characters is %s\n", 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) //读线程two
{
printf("thread read two 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\n", 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) //写线程one
{
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);
printf("this is write thread two try to get lock\n");
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);
}
</span>