Linux--多线程之读写锁的使用以及demo

30 篇文章 7 订阅

读写锁

  • 如果当前线程读数据 则允许其他线程进行读操作 但不允许写操作
  • 如果当前线程写数据 则其他线程的读写都不允许操作

*在mutex的基础上 区分了 读锁定 和写锁定
如果某线程申请了读锁定 其他线程依旧可以申请读锁 不能申请写锁定
如果某线程申请了写锁定 则其他线程不能申请读锁定 也不能申请写锁定

读写锁对象的创建
pthread_rwlock_t  rwlock;
读写锁相关函数
函数描述
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)读写锁的销毁
int pthread_rwlock_init(pthread_rwlock_t * rwlock, const pthread_rwlockattr_t *restrict attr);初始化读写锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);读锁定
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);尝试读锁定
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);尝试写锁定
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);写锁定

参数:

pthread_rwlock_t *rwlock:读写锁的地址

const pthread_rwlockattr_t *restrict attr:读写锁的属性

demo
#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);     //a线程的执行读函数
void *thread_function_read_t(void *arg);    //b线程的执行读函数
void *thread_function_write_o(void *arg);	//c线程的执行写函数
void *thread_function_write_t(void *arg);	//d线程的执行写函数
	

int main(int argc,char *argv[]) 
{
	int res;
	pthread_t a_thread,b_thread,c_thread,d_thread;  //定义a,b,c,d四个线程的ID
	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);		//等待线程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_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);
}

执行效果及分析

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值