Linux多线程4-2_读写锁

一、读写锁的概念

1、读写锁与互斥量类似,不过读写锁有更高的并行性。互斥量要么加锁要么不加锁,而且同一时刻只允许一个线程对其加锁。对于一个变量的读取, 完全可以让多个线程同时进行操作。
2、pthread_rwlock_t rwlock
读写锁有三种状态,读模式下加锁,写模式下加锁,不加锁。一次只有一个线程可以占有写模式下的读写锁,但是多个线程可以同时占有读模式的读写锁。
3、读写锁在写加锁状态时,在它被解锁之前,所有试图对这个锁加锁的线程都会阻塞。

读写锁在读加锁状态时,所有试图以读模式对其加锁的线程都会获得访问权,但是如果线程希望以写模式对其加锁,它必须阻塞直到所有的线程释放锁。

当读写锁一读模式加锁时,如果有线程试图以写模式对其加锁,那么读写锁会阻塞随后的读模式锁请求。这样可以避免读锁长期占用,而写锁达不到请求。

4、读写锁非常适合对数据结构读次数大于写次数的程序,当它以读模式锁住时,是以共享的方式锁住的;当它以写模式锁住时,是以独占的模式锁 住的。

二、读写锁的初始化

1、读写锁在使用之前必须初始化
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
2、 使用完需要销毁
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
成功返回0 ,失败返回错误码

三、加锁和解锁

1、读模式加锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
2、写模式加锁
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
3、解锁
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
成功返回0

四、实例

1、程序架构

2、源代码

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>
#include <queue>


#include "include/pthread.h"



#ifndef _WIN64
#pragma comment(lib,".\\lib32\\pthreadVC2.lib")
#else
#pragma comment(lib,".\\lib64\\pthreadVC2.lib")
#endif 

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


读写锁在读加锁状态时,所有试图以读模式对其加锁的线程都会获得访问权,但是如果线程希望以写
模式对其加锁,它必须阻塞直到所有的线程释放锁。

当读写锁一读模式加锁时,如果有线程试图以写模式对其加锁,那么读写锁会阻塞随后的读模式锁请求。
这样可以避免读锁长期占用,而写锁达不到请求。

当它以读模式锁住时,是以共享的方式锁住的;当它以写模式锁住时,是以独占的模式锁 住的。
*/

/*
*DESCRIPTION: 验证可以有多个线程同时拥有读模式下到读写锁
*    读写锁在使用之前必须初始化
*    int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
*     const pthread_rwlockattr_t *restrict attr);
*    成功返回0 ,失败返回错误码
*
*    使用完需要销毁
*    int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
*    成功返回0 ,失败返回错误码
*
*    读模式加锁
*    int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
*    int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
*    写模式加锁
*    int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
*    int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
*    解锁
*    int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
*/

pthread_rwlock_t rwlock;
int num = 0;

void *thread_fun1(void *arg)
{
	int err;

	pthread_rwlock_rdlock(&rwlock);
	printf("thread 1 print num %d\n", num);
	Sleep(5);
	printf("thread 1 over\n");

	pthread_rwlock_unlock(&rwlock);

	return (void *)1;
}

void *thread_fun2(void *arg)
{
	int err;

	pthread_rwlock_wrlock(&rwlock);
	printf("thread 2 print num %d\n", num);
	Sleep(5);
	printf("thread 2 is over\n");
	pthread_rwlock_unlock(&rwlock);

	return (void *)1;
}

int main()
{
	pthread_t tid1, tid2;
	int err;

	//初始化读写锁。
	err = pthread_rwlock_init(&rwlock, NULL);
	if (err)
	{
		printf("init rwlock failed\n");
		return 0;
	}

	//创造新线程1
	err = pthread_create(&tid1, NULL, thread_fun1, NULL);
	if (err)
	{
		printf("create new thread 1 failed\n");
		return 0;
	}
	//创造新线程2
	err = pthread_create(&tid2, NULL, thread_fun2, NULL);
	if (err)
	{
		printf("create new thread 2 failed\n");
		return 0;
	}

	//阻塞线程,等待执行完成
	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	
	//销毁读写锁
	pthread_rwlock_destroy(&rwlock);

	system("pause");
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值