一.概述                                                   

读写锁与互斥量的功能类似,对临界区的共享资源进行保护!互斥量一次只让一个线程进入临界区,读写锁比它有更高的并行性。读写锁有以下特点:

1.如果一个线程用读锁锁定了临界区,那么其他线程也可以用读锁来进入临界区,这样就可以多个线程并行操作。但这个时候,如果再进行写锁加锁就会发生阻塞,写锁请求阻塞后,后面如果继续有读锁来请求,这些后来的读锁都会被阻塞!这样避免了读锁长期占用资源,防止写锁饥饿!

2.如果一个线程用写锁锁住了临界区,那么其他线程不管是读锁还是写锁都会发生阻塞!

读写锁的优势往往展现在读操作很频繁,而写操作较少的情况下

二.函数接口                                           

1.创建读写锁

1.1:宏常量初始化

1 pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;

1.2:函数初始化

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

rwlock:读写锁的pthread_rwlock_t结构指针

attr:读写锁的属性结构指针。不需要别的属性默认为NULL。

2.读写锁加锁与解锁

1 #include <pthread.h>
2
3 int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
4 int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
5 int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

rwlock:创建的读写锁指针

3.其他类型的加锁

1 #include <pthread.h>
2 #include <time.h>
3
4
5 int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
6 int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
7
8 int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abs_timeout);
9 int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abs_timeout);

try类函数加锁:如果获取不到锁,会立即返回错误EBUSY!

timed类函数加锁:如果规定的时间内获取不到锁,会返回ETIMEDOUT错误!

4.销毁读写锁

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

应用实例:

创建4个线程,2个线程读锁,2个线程写锁,观察4个线程进入临界区的顺序:

 /**
* * @file pthread_rwlock.c
* */ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
using namespace std;
/* 初始化读写锁 */
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
/* 全局资源 */
class CSingle{
public:
static CSingle& instance(){
static CSingle single;
return single;
}
void setX(int y){
this->x = y;
}
int getX(){
return this->x;
}
int x;
};
int global_num = ; void err_exit(const char *err_msg)
{
printf("error:%s\n", err_msg);
exit();
} /* 读锁线程函数 */
void *thread_read_lock(void *arg)
{
char *pthr_name = (char *)arg; while (global_num)
{
/* 读加锁 */
pthread_rwlock_rdlock(&rwlock); printf("线程%s进入临界区,global_num = %d, X:%d\n", pthr_name, global_num, CSingle::instance().getX());
sleep();
printf("线程%s离开临界区...\n", pthr_name); /* 读解锁 */
pthread_rwlock_unlock(&rwlock); sleep();
} return NULL;
} /* 写锁线程函数 */
void *thread_write_lock(void *arg)
{
char *pthr_name = (char *)arg; while (global_num)
{
/* 写加锁 */
pthread_rwlock_wrlock(&rwlock); /* 写操作 */
--global_num;
CSingle::instance().setX(global_num);
printf("线程%s进入临界区,global_num = %d, X:%d\n", pthr_name, global_num, CSingle::instance().getX());
sleep();
printf("线程%s离开临界区...\n", pthr_name); /* 写解锁 */
pthread_rwlock_unlock(&rwlock); sleep();
} return NULL;
} int main(void)
{
pthread_t tid_read_1, tid_read_2, tid_write_1, tid_write_2; /* 创建4个线程,2个读,2个写 */
if (pthread_create(&tid_read_1, NULL, thread_read_lock, (void *)"read_1") != )
err_exit("create tid_read_1"); if (pthread_create(&tid_read_2, NULL, thread_read_lock, (void *)("read_2")) != )
err_exit("create tid_read_2"); if (pthread_create(&tid_write_1, NULL, thread_write_lock, (void *)("write_1")) != )
err_exit("create tid_write_1"); if (pthread_create(&tid_write_2, NULL, thread_write_lock, (void *)("write_2")) != )
err_exit("create tid_write_2"); /* 随便等待一个线程,防止main结束 */
if (pthread_join(tid_read_1, NULL) != )
err_exit("pthread_join()");
if (pthread_join(tid_read_2, NULL) != )
err_exit("pthread_join()");
if (pthread_join(tid_write_1, NULL) != )
err_exit("pthread_join()");
if (pthread_join(tid_write_2, NULL) != )
err_exit("pthread_join()"); return ;
}

linux读写锁

linux读写锁的更多相关文章

  1. linux 读写锁应用实例

    转自:http://blog.csdn.net/dsg333/article/details/22113489 /*使用读写锁实现四个线程读写一段程序的实例,共创建了四个新的线程,其中两个线程用来读取 ...

  2. Linux读写锁的使用

    读写锁是用来解决读者写者问题的,读操作可以共享,写操作是排它的,读可以有多个在读,写只有唯一个在写,写的时候不允许读. 具有强读者同步和强写者同步两种形式: 强读者同步:当写者没有进行写操作时,读者就 ...

  3. Linux 读写锁

    线程的读写锁函数: 1,读写锁的初始化与销毁,静态初始化的话,可以直接使用PTHREAD_RWLOCK_INITIALIZER. #include <pthread.h> int pthr ...

  4. Linux的线程同步对象:互斥量Mutex,读写锁,条件变量

        进程是Linux资源分配的对象,Linux会为进程分配虚拟内存(4G)和文件句柄等 资源,是一个静态的概念.线程是CPU调度的对象,是一个动态的概念.一个进程之中至少包含有一个或者多个线程.这 ...

  5. linux中读写锁的rwlock介绍-nk_ysg-ChinaUnix博客

    linux中读写锁的rwlock介绍-nk_ysg-ChinaUnix博客 linux中读写锁的rwlock介绍 2013-02-26 13:59:35 分类: C/C++   http://yaro ...

  6. linux 内核的另一个自旋锁 - 读写锁

    除spinlock外,linux 内核还有一个自旋锁,名为arch_rwlock_t.它的头文件是qrwlock.h,包含在spinlock.h,头文件中对它全称为"Queue read/w ...

  7. linux线程间同步(1)读写锁

    读写锁比mutex有更高的适用性,能够多个线程同一时候占用读模式的读写锁.可是仅仅能一个线程占用写模式的读写锁. 1. 当读写锁是写加锁状态时,在这个锁被解锁之前,全部试图对这个锁加锁的线程都会被堵塞 ...

  8. Linux系统编程 —读写锁rwlock

    读写锁是另一种实现线程间同步的方式.与互斥量类似,但读写锁将操作分为读.写两种方式,可以多个线程同时占用读模式的读写锁,这样使得读写锁具有更高的并行性. 读写锁的特性为:写独占,读共享:写锁优先级高. ...

  9. linux kernel RCU 以及读写锁

    信号量有一个很明显的缺点,没有区分临界区的读写属性,读写锁允许多个线程进程并发的访问临界区,但是写访问只限于一个线程,在多处理器系统中允许多个读者访问共享资源,但是写者有排他性,读写锁的特性如下:允许 ...

随机推荐

  1. 列表生成式+过滤器(filter)+映射(map)+lambda总结

    这些都是python的特色,不仅强大,而且好用,配合起来使用更是无敌. 零.lambda lambda用于产生一个匿名表达式,组成部分为:lambda + ‘函数表达式’ ‘函数表达式’由一个冒号加上 ...

  2. MFC Timer定时器

    知识点: 定时器Timer 创建定时器 销毁定时器 代码测试 一. 创建定时器 UINT SetTimer( HWND hWnd, // 指定关联定时器的窗口句柄,在MFC版将省略此参数 UINT n ...

  3. IP 解析器(IpParser) test 和 生产环境 实现

    注意:之前我maven居然没有引入 StringUtils 的包,然后引入了一个路径类似,但其实包路径不一样的 StringUtils ,居然是划掉的状态,像这样 StringUtils ,这个其实不 ...

  4. 使用Redis做分布式

    一 为什么使用 Redis 在项目中使用 Redis,主要考虑两个角度:性能和并发.如果只是为了分布式锁这些其他功能,还有其他中间件 Zookpeer 等代替,并非一定要使用 Redis. 性能: 如 ...

  5. Salesforce随笔: 解决被指定给Chatter相关用户的RecordType无法被删除的问题

    被指定给以下三组用户的RecordType无法在对应的Profile里取消占用: Chatter External User Chatter Free User Chatter Moderator U ...

  6. 学习git 新手。这个写的不错

    转自:https://www.cnblogs.com/wupeiqi/p/7295372.html 版本控制 说到版本控制,脑海里总会浮现大学毕业是写毕业论文的场景,你电脑上的毕业论文一定出现过这番景 ...

  7. mysql学习(2)-Navicat Premium 12 链接MySQL8.0.11数据库报2059错误

    Navicat Premium 12 链接MySQL8.0.11数据库报2059错误 1,问题现象 安装完MySQL8.0.11和Navicat Premium12后,我们会用Navicat去测试连接 ...

  8. IDA Pro 在CSAPP lab2中的使用

    在做lab2的时候,偶然的情况下,发现了IDA pro这样一个反汇编工具,总的来说对于lab2这样的小实验读懂代码的大体功能是有作用的,但对于想要具体明白某一条指令的执行过程,又显得不足,到最后还是需 ...

  9. uc浏览器app点评

    uc浏览器app我经常用,是我接触的第一款手机浏览器,感觉还不错的,uc浏览器新闻更新速度有点慢,有时候还闪退,以前在搜索栏粘贴文字后,如果想改后面的文字,根本就不行,用uc浏览器下东西速度比较慢,现 ...

  10. Daily Scrum NO.1

    工作概况 符美潇(PM): 今日工作 1.根据开发进程分配第一步开发工作,对相应的成员提出今日的开发要求:要求成员自己所负责的线程池,动态爬取,去重,文件分类等部分进行资料的相关了解. 2.Daily ...