一、什么是读写锁
读写锁(也叫共享-独占锁)实际是一种特殊的自旋锁,它把对共享资源的访问者划分成读者和写者,读者只对共享资源进行读访问,写者则需要对共享资源进行写操作。这种锁相对于自旋锁而言,能提高并发性,因为在多处理器系统中,它允许同时有多个读者来访问共享资源,最大可能的读者数为实际的逻辑CPU数。写者是排他性的,一个读写锁同时只能有一个写者或多个读者(与CPU数相关),但不能同时既有读者又有写者。
如果读写锁当前没有读者,也没有写者,那么写者可以立刻获得读写锁,否则它必须“自旋”在那里,直到没有任何写者或读者。如果读写锁没有写者,那么读者可以立即获得该读写锁,否则读者必须“自旋”在那里,直到写者释放该读写锁。读写锁适合于对数据结构的读次数比写次数多很多的场合。
二、示例代码
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
/* 线程控制块 */
static pthread_t reader1;
static pthread_t reader2;
static pthread_t writer1;
/* 共享数据book */
static int book = 0;
/* 读写锁 */
static pthread_rwlock_t rwlock;
/* 函数结果检查 */
static void check_result(char* str,int result)
{
if (0 == result)
{
printf("%s successfully!\n",str);
}
else
{
printf("%s failed! error code is %d\n",str,result);
}
}
/*线程入口*/
static void* reader1_entry(void* parameter)
{
int i = 0;
//pthread_detach(pthread_self());
while (1)
{
if(i++%5<2)//读2秒,停3秒。让写可获得锁,否则读线程一直锁着,将无法写
{
pthread_rwlock_rdlock(&rwlock); /* 尝试读锁定该读写锁 */
printf("reader1 read book value is %d\n",book);
sleep(1); /* 线程休眠2秒,切换到其他线程运行 */
pthread_rwlock_unlock(&rwlock); /* 线程运行后对读写锁解锁 */
}else
{
sleep(1);
}
}
}
static void* reader2_entry(void* parameter)
{
int i = 0;
//pthread_detach(pthread_self());
while (1)
{
if(i++%5<2)
{
pthread_rwlock_rdlock(&rwlock); /* 尝试读锁定该读写锁 */
printf("reader2 read book value is %d\n",book);
sleep(1); /* 线程休眠2秒,切换到其他线程运行 */
pthread_rwlock_unlock(&rwlock); /* 线程运行后对读写锁解锁 */
}else
{
sleep(1); /* 线程休眠2秒,切换到其他线程运行 */
}
}
}
static void* writer1_entry(void* parameter)
{
//pthread_detach(pthread_self());
while (1)
{
//printf("writer1 \r\n");
if(!pthread_rwlock_trywrlock(&rwlock)) /* 尝试写锁定该读写锁 */
{
book++;
printf("writer1 write book value is %d\n",book);
pthread_rwlock_unlock(&rwlock); /* 对读写锁解锁 */
sleep(1); /* 线程休眠2秒,切换到其他线程运行 */
}else
{
printf("Be BUSY ,can`t write now\r\n");
sleep(1);
}
}
}
/* 用户应用入口 */
int application_init()
{
int result;
/* 默认属性初始化读写锁 */
pthread_rwlock_init(&rwlock,NULL);
/*创建writer1线程,线程入口是writer1_entry, 线程属性为,入口参数为NULL*/
result = pthread_create(&writer1,NULL,writer1_entry,NULL);
check_result("writer1 created",result);
/*创建reader1线程,线程入口是reader1_entry, 线程属性为默认值,入口参数为NULL*/
result = pthread_create(&reader1,NULL,reader1_entry,NULL);
check_result("reader1 created",result);
/*创建reader2线程,线程入口是reader2_entry, 线程属性为默认值,入口参数为NULL*/
result = pthread_create(&reader2,NULL,reader2_entry,NULL);
check_result("reader2 created",result);
return 0;
}
int main()
{
int i ;
application_init();
i=100;
do{
sleep(1);
}while(i--);
}
运行结果:
-bash-3.2$ !g
gcc -pthread rwlock.c -o app
-bash-3.2$ ./app
writer1 created successfully!
reader1 created successfully!
reader2 created successfully!
writer1 write book value is 1
reader1 read book value is 1
reader2 read book value is 1
Be BUSY ,cant write now
reader1 read book value is 1
reader2 read book value is 1
Be BUSY ,cant write now
writer1 write book value is 2
writer1 write book value is 3
reader2 read book value is 3
reader1 read book value is 3
Be BUSY ,cant write now
reader2 read book value is 3
reader1 read book value is 3
Be BUSY ,cant write now
writer1 write book value is 4
writer1 write book value is 5
writer1 write book value is 6
reader2 read book value is 6
reader1 read book value is 6
Be BUSY ,cant write now
reader2 read book value is 6
reader1 read book value is 6
Be BUSY ,cant write now
writer1 write book value is 7
writer1 write book value is 8
writer1 write book value is 9
reader2 read book value is 9
Be BUSY ,can`t write now
reader1 read book value is 9