Linux线程间通信之读写锁(十六)

1.读写锁概述

当有一个线程已经持有互斥锁时,互斥锁将所有试图进入临界区的线程都阻塞住。但是考虑一种情形,当前持有互斥锁的线程只是要读访问共享资源,而同时有其它几个线程也想读取这个共享资源,但是由于互斥锁的排它性,所有其它线程都无法获取锁,也就无法读访问共享资源了,但是实际上多个线程同时读访问共享资源并不会导致问题。 在对数据的读写操作中,更多的是读操作,写操作较少,例如对数据库数据的读写应用。为了满足当前能够允许多个读出,但只允许一个写入的需求,线程提供了读写锁来实现。

读写锁的特点如下:
1)如果有其它线程读数据,则允许其它线程执行读操作,但不允许写操作。
2)如果有其它线程写数据,则其它线程都不允许读、写操作。

读写锁分为读锁和写锁,规则如下:
1)如果某线程申请了读锁,其它线程可以再申请读锁,但不能申请写锁。
2)如果某线程申请了写锁,其它线程不能申请读锁,也不能申请写锁。

POSIX 定义的读写锁的数据类型是: pthreadrwlockt。

2.初始化读写锁函数

	int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,const pthread_rwlockattr_t *restrict attr);

功能:
用来初始化 rwlock 所指向的读写锁。

参数:
rwlock:指向要初始化的读写锁指针。
attr:读写锁的属性指针。如果 attr 为 NULL 则会使用默认的属性初始化读写锁,否则使用指定的 attr 初始化读写锁。

说明:
可以使用宏 PTHREAD_RWLOCK_INITIALIZER 静态初始化读写锁,比如:
pthread_rwlock_t my_rwlock = PTHREAD_RWLOCK_INITIALIZER;
这种方法等价于使用 NULL 指定的 attr 参数调用 pthread_rwlock_init() 来完成动态初始化,不同之处在于PTHREAD_RWLOCK_INITIALIZER 宏不进行错误检查。

返回值:
成功:0,读写锁的状态将成为已初始化和已解锁。
失败: 非0 ,错误码。

3.释放读写锁函数

	int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

功能:
用于销毁一个读写锁,并释放所有相关联的资源(所谓的所有指的是由 pthread_rwlock_init() 自动申请的资源) 。

参数:
rwlock:读写锁指针。

返回值:
成功:0。
失败: 非0 ,错误码。

4.申请读锁函数

	int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);

功能:
以阻塞方式在读写锁上获取读锁(读锁定)。
如果没有写者持有该锁,并且没有写者阻塞在该锁上,则调用线程会获取读锁。
如果调用线程未获取读锁,则它将阻塞直到它获取了该锁。一个线程可以在一个读写锁上多次执行读锁定。

线程可以成功调用 pthread_rwlock_rdlock() 函数 n 次,但是之后该线程必须调用 pthread_rwlock_unlock() 函数 n 次才能解除锁定。

参数:
rwlock:读写锁指针。

返回值:
成功:0。
失败: 非0 ,错误码。

说明:

	int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
	用于尝试以非阻塞的方式来在读写锁上获取读锁。 
	如果有任何的写者持有该锁或有写者阻塞在该读写锁上,则立即失败返回。

5.申请写锁函数

	int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);

功能:
在读写锁上获取写锁(写锁定)。
如果没有写者持有该锁,并且没有写者读者持有该锁,则调用线程会获取写锁。
如果调用线程未获取写锁,则它将阻塞直到它获取了该锁。

参数:
rwlock:读写锁指针。

返回值:
成功:0。
失败: 非0 ,错误码。

说明:

	int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
	用于尝试以非阻塞的方式来在读写锁上获取写锁。
	如果有任何的读者或写者持有该锁,则立即失败返回。

6.释放读写锁函数

	int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

功能:
无论是读锁或写锁,都可以通过此函数解锁。

参数:
rwlock:读写锁指针。

返回值:
成功:0。
失败: 非0 ,错误码。

7.参考代码

//=============================================================================
// File Name    : thread_mutex_rwlock.c
// Author       : FengQQ
//
// Description  : 读写锁
// Annotation   : 1)如果有其它线程读数据,则允许其它线程执行读操作,但不允许写操作。
//				  2)如果有其它线程写数据,则其它线程都不允许读、写操作。
//
// Created by FengQQ. 2020-10-04
//=============================================================================
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>

pthread_rwlock_t rwlock;
static int num=0;

//---------------线程1入口函数---------------
void *pthread1_callback(void *arg)
{
    
    
	while(1)
	{
    
    
		pthread_rwlock_rdlock(&rwlock);		//加锁
		printf("pthread 1 print num %d\r\n",num++);
		sleep(5);
		printf("pthread 1 over\r\n");	
		pthread_rwlock_unlock(&rwlock);		//解锁
	}
	
}
//---------------线程2入口函数---------------
void *pthread2_callback(void *arg)
{
    
    
	while(1)
	{
    
    
		pthread_rwlock_rdlock(&rwlock);		//加锁
		printf("pthread 2 print num %d\r\n",num++);
		sleep(5);
		printf("pthread 2 over\r\n");
		pthread_rwlock_unlock(&rwlock);		//解锁
	}	
}

int main(int argc,char *argv[])
{
    
    
	int ret;
	pthread_t ptid1,ptid2;
	
	ret = pthread_rwlock_init(&rwlock,NULL);						//互斥锁初始化
	if(ret != 0)
	{
    
    
		printf("pthread rwlock init failed...\r\n");
		return -1;
	}
	
	ret = pthread_create(&ptid1,NULL,pthread1_callback,	NULL);		//创建线程1
	if(ret != 0)
	{
    
    
		printf("create new pthread1 failed...\r\n");
		return -1;
	}
	
	ret = pthread_create(&ptid2,NULL,pthread2_callback,	NULL);		//创建线程2
	if(ret != 0)
	{
    
    
		printf("create new pthread2 failed...\r\n");
		return -1;
	}
	
	ret = pthread_join(ptid1,NULL);									//回收线程1资源
	if(ret != 0)
	{
    
    
		printf("pthread1 join failed...\r\n");
	}
	
	ret = pthread_join(ptid2,NULL);									//回收线程2资源
	if(ret != 0)
	{
    
    
		printf("pthread2 join failed...\r\n");
	}
	
	pthread_rwlock_destroy(&rwlock);
	
	return 0;
}

Linux线程间通信之条件变量(十七)

链接: link.(https://blog.csdn.net/qq_39721016/article/details/120477932?spm=1001.2014.3001.5501)

猜你喜欢

转载自blog.csdn.net/qq_39721016/article/details/120477798
今日推荐