linux 读写锁

读写锁有三种状态:

(1)读模式下枷锁状态

(2)写模式下加锁状态

(3)不加锁状态

一次只有一个线程可以占有写模式的读写锁,但是多个线程可以同时占有读模式的读写锁。

当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞。

当读写锁在读加锁状态时,所以试图读模式对它进行加锁的线程都可以得到访问权,但是任何希望以写模式对此锁进行加锁的线程都会被阻塞,直到所有的线程释放它们的锁为止。

当读写锁在读加锁状态时,而这时有一个线程试图以写模式火区锁时,读写锁通常会阻塞随后的读模式请求。这样子可以避免读模式锁长期占用,而等待的写模式锁请求一直得不到满足。


初始化与销毁:

#include <pthread.h>
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

加锁及解锁:

int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock); 


读写锁原语:

#include <pthread.h>
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

一个demo:

#include <stdio.h>  
#include <pthread.h>  
#include <unistd.h>

#define Read_Num 2  

pthread_rwlock_t lock;  

class Data  
{
public:  
	Data(int i, float f): I(i),F(f)  
	{}
public:
	int I;
	float F;
};

Data *pdata = NULL;


void *read(void *arg)
{
	int id = (int)arg;
	while(true)
	{
		pthread_rwlock_rdlock(&lock);
		printf(" reader %d is reading data!\n", id);
		if(pdata == NULL)
		{
			printf("data is NULL\n");
		}
		else
		{
			printf("data: I = %d, F = %f \n", pdata->I, pdata->F);
		}
		pthread_rwlock_unlock(&lock);
	}

	pthread_exit(0);
}  

void *write(void *arg)
{
	while(true)
	{
		pthread_rwlock_wrlock(&lock);
		printf(" writer is writind data!\n");
		if(pdata == NULL)
		{
			pdata = new Data(1, 1.1);
			printf("Writer is writing data: %d, %f\n", pdata->I, pdata->F);
		}
		else  
		{
			delete pdata;
			pdata = NULL;
			printf("writer free the data!");
		}

		pthread_rwlock_unlock(&lock);
	}
	pthread_exit(0);
}  


int main()  
{  
	pthread_t reader[Read_Num];
	pthread_t writer;

	for(int i = 0;i<Read_Num;i++)
	{
		pthread_create(&reader[i], NULL, read, (void *)i);
	}

	pthread_create(&writer, NULL, write, NULL);

	sleep(10);

	return 0;
}  
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

另一个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_1(void *arg);//读线程1
void *thread_function_read_2(void *arg);//读线程2
void *thread_function_write_1(void *arg);//写线程1
void *thread_function_write_2(void *arg);//写线程2


int main()
{
	int res;
	pthread_t thread1,thread2,thread3,thread4;
	void *thread_result;

	res=pthread_rwlock_init(&rwlock, NULL);//初始化读写锁
	if (res != 0)
	{
		perror("rwlock initialization failed");
		exit(EXIT_FAILURE);
	}
	res = pthread_create(&thread1, NULL, thread_function_read_1, NULL);//create new thread创建线程
	if (res != 0)
	{
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}

	res = pthread_create(&thread2, NULL, thread_function_read_2, NULL);//create new thread
	if (res != 0)
	{
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}
	res = pthread_create(&thread3, NULL, thread_function_write_1, NULL);//create new thread
	if (res != 0)
	{
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}
	res = pthread_create(&thread4, NULL, thread_function_write_2, NULL);//create new thread
	if (res != 0)
	{
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}

	res = pthread_join(thread1, &thread_result);//等待a_thread线程结束           
	if (res != 0)
	{
		perror("Thread join failed");
		exit(EXIT_FAILURE);
	}
	res = pthread_join(thread2, &thread_result);           
	if (res != 0)
	{
		perror("Thread join failed");
		exit(EXIT_FAILURE);
	}
	res = pthread_join(thread3, &thread_result);           
	if (res != 0)
	{
		perror("Thread join failed");
		exit(EXIT_FAILURE);
	}
	res = pthread_join(thread4, &thread_result);           
	if (res != 0)
	{
		perror("Thread join failed");
		exit(EXIT_FAILURE);
	}

	pthread_rwlock_destroy(&rwlock);//销毁读写锁               
	exit(EXIT_SUCCESS);
}

void *thread_function_read_1(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_2(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_1(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_2(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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值