pthread_rwlock pthread读写锁
原文: http://www.cnblogs.com/diegodu/p/3890450.html
使用读写锁
配置读写锁的属性之后,即可初始化读写锁。以下函数用于初始化或销毁读写锁、锁定或解除锁定读写锁或尝试锁定读写锁。下表列出了本节中讨论的用来处理读写锁的函数。
表 4–9 处理读写锁的例程
|
操作 |
相关函数说明 |
|---|---|
|
初始化读写锁 |
|
|
读取读写锁中的锁 |
|
|
读取非阻塞读写锁中的锁 |
|
|
写入读写锁中的锁 |
|
|
写入非阻塞读写锁中的锁 |
|
|
解除锁定读写锁 |
|
|
销毁读写锁 |
---------------------------------------------------------------------------------------
1、概述
读写锁与互斥量类似,不过读写锁允许更高的并行性。互斥量要么是锁住状态,要么是不加锁状态,而且一次只有一个线程对其加锁。读写锁可以有三种状态:读模式下加锁状态,写模式下加锁状态,不加锁状态。一次只有一个线程可以占有写模式的读写锁,但是多个线程可用同时占有读模式的读写锁。读写锁也叫做共享-独占锁,当读写锁以读模式锁住时,它是以共享模式锁住的,当它以写模式锁住时,它是以独占模式锁住的。
2、读写锁API
读写锁的数据类型为pthread_rwlock_t。如果这个类型的某个变量是静态分配的,那么可通过给它赋常值PTHREAD_RWLOCK_INITIALIZER来初始化它。
获取和释放读写锁:
int pthread_rwlock_rdlock(pthread_rwlock_t *rwptr); //获取一个读出锁
int pthread_rwlock_wrlock(pthread_rwlock_t *rwptr); //获取一个写入锁
int pthread_rwlock_unlock(pthread_rwlock_t *rwptr); //释放一个写入锁或者读出锁
都返回:成功时为0,出错时为正的Exxx值
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwptr);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwptr);
都返回:成功时为0,出错时为正的Exxx值
读写锁属性:
int pthread_rwlock_init(pthread_rwlock_t *rwptr, const pthread_rwlockattr_t *attr)
int pthread_rwlock_destroy(pthread_rwlock_t *rwptr);
都返回:成功时为0,出错时为正的Exxx值
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
都返回:成功时为0,出错时为正的Exxx值
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, int *valptr);
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int valptr);
都返回:成功时为0,出错时为正的Exxx值
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h> #define MAXDATA 1024
#define MAXREDER 100
#define MAXWRITER 100
struct
{
pthread_rwlock_t rwlock;
char datas[MAXDATA];
} shared = {
PTHREAD_RWLOCK_INITIALIZER
}; void *reader(void *arg);
void *writer(void *arg); int main(int argc,char *argv[])
{
int i,readercount,writercount;
pthread_t tid_reader[MAXREDER],tid_writer[MAXWRITER];
if(argc != 3)
{
printf("usage : <reader_writer> #<readercount> #<writercount>\n");
exit(0);
}
readercount = atoi(argv[1]);
writercount = atoi(argv[2]);
pthread_setconcurrency(readercount+writercount);
for(i=0;i<writercount;++i)
pthread_create(&tid_writer[i],NULL,writer,NULL);
sleep(1);
for(i=0;i<readercount;++i)
pthread_create(&tid_reader[i],NULL,reader,NULL);
for(i=0;i<writercount;++i)
pthread_join(tid_writer[i],NULL);
for(i=0;i<readercount;++i)
pthread_join(tid_reader[i],NULL);
exit(0);
}
void *reader(void *arg)
{
pthread_rwlock_rdlock(&shared.rwlock);
printf("Reader begins read message.\n");
sleep(1);
printf("Read message is: %s\n",shared.datas);
pthread_rwlock_unlock(&shared.rwlock);
return NULL;
} void *writer(void *arg)
{
char datas[MAXDATA];
pthread_rwlock_wrlock(&shared.rwlock);
printf("Writers begings write message.\n");
printf("Enter the write message: \n");
gets(datas);
strcat(shared.datas,datas);
pthread_rwlock_unlock(&shared.rwlock);
return NULL;
}
打印输出


[root@localhost pthread_rwlock]# ./run 5 1
Writers begings write message.
Enter the write message:
sdfs
Reader begins read message.
Reader begins read message.
Reader begins read message.
Reader begins read message.
Reader begins read message.
Read message is: sdfs
Read message is: sdfs
Read message is: sdfs
Read message is: sdfs
Read message is: sdfs
[root@localhost pthread_rwlock]#

我在read中加入sleep,看家其他read线程也进入了,表明一次只有一个线程可以占有写模式的读写锁,但是多个线程可用同时占有读模式的读写锁。
另外请关注pthread_setconcurrency();
最近在code review一些人的代码的时候,发现了一个问题,就是很少人关注pthread_setconcurrency()函数,其实这个函数在pthread中是一个很重要的函数。在linux下,如果你忽略了这个函数的使用,那么能够并发的线程数目由实现者来控制,对于系统调度的效率而言往往不是什么好的事情,因为默认的设置往往不是最佳的。
更为糟糕的是,如果在某些系统中,如果你不调用pthread_setconcurrency()函数,那么系统中的运行的线程仅仅是第一个被创建的线程,其他线程根本不会被运行
。比如在solaris 2。6中就有这些情况。为了在unix或者是linux系统上使移植更加的容易,请不要忘记在适当的地方调用次函数,清晰的告诉系统我们使用的线程个数。虽然在某些系统上,这个调用是徒劳的,但是它的使用增强的移植性!
pthread_rwlock pthread读写锁的更多相关文章
- 【C/C++多线程编程之九】pthread读写锁
多线程编程之读写锁 Pthread是 POSIX threads 的简称,是POSIX的线程标准. pthread读写锁把对共享资源的訪问者分为读者和写者,读者仅仅对共享资源 ...
- pthread 读写锁
pthread 读写锁 (Read Write Lock, rwlock) 把对共享资源的访问者分为读者和写者,读者仅仅对共享资源进行读访问,写者仅仅对共享资源进行写操作. 如果使用互斥量 mutex ...
- c++ 读写锁
#ifndef THREAD_UTIL_H #define THREAD_UTIL_H #include <pthread.h> namespace spider { class Auto ...
- pthread中读写锁
读写锁很像一个互斥量,他阻止多个线程同时修改共享数据的另一种方法,区分不同互斥量的是他是分读数据和写数据,一个读写锁允许同时多个线程读数据,只要他们不修改数据. 只要没有写模式下的加锁,任意线程都可以 ...
- 基于pthread实现读写锁
读写锁可用于在多线程访问map等数据结构时使用 #include <pthread.h> class ReadWriteLock { public: ReadWriteLock() { p ...
- 读写锁(pthread)
读写锁: 用于对于某个给定资源的共享访问,而不是像互斥锁那样,将所有试图进入临界区的线程都阻塞住 相关内容: 线程互斥锁 分配规则:(写独占,读共享) 1.只要没有线程持有某个给定的读写锁用于写,那么 ...
- linux线程同步(3)-读写锁
一.概述 读写锁与互斥量的功能类似,对临界区的共享资源进行保护!互斥量一次只让一个线程进入临界区, ...
- linux读写锁
一.概述 读写锁与互斥量的功能类似,对临界区的共享资源进行保护!互斥量一次只让一个线程进入临界区, ...
- C基础 读写锁中级剖析
引言 读写锁 是为了 解决, 大量 ''读'' 和 少量 ''写'' 的业务而设计的. 读写锁有3个特征: 1.当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞 2.当 ...
随机推荐
- akka设计模式系列-Chain模式
链式调用在很多框架和系统中经常存在,算不得上是我自己总结的设计模式,此处只是简单介绍在Akka中的两种实现方式.我在这边博客中简化了链式调用的场景,简化后也更符合Akka的设计哲学. trait Ch ...
- $ST表刷题记录$
\(st表的题目不太多\) 我做过的就这些吧. https://www.luogu.org/problemnew/show/P3865 https://www.luogu.org/problemnew ...
- ORA-01012:not logged on的解决办法
conn / as sysdba 报错ORA-01012: not logged on 发生原因:关闭数据库是shutdown 后面没有接关闭参数中的任何一个. nomal ————- —-所有连接都 ...
- ActiveMQ 5.10.0 安装与配置
先在官网下载activeMQ,我这里是5.10.0. 然后在解压在一个文件夹下即可. 我这里是:D:\apache-activemq-5.10.0-bin 然后进入bin目录:D:\apache-ac ...
- PDO访问其他数据库操作及作用
PDO的作用 PDO<!--数据访问抽象层--><!--1.可以访问其他数据库--><!--2.具有事务功能--><!--3.带有预处理语句功能(防止SQL注 ...
- 【MySQL】二进制分发安装
操作系统:Red Hat Enterprise Linux Server release 6.5 Mysql安装包:mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz ...
- JS——sort
1.a-b升序 <script> var arr = [2, 3, 1, 5, 0]; function compare(a, b) { return a - b; } console.l ...
- 控制台——EventLog实现事件日志操作
我们应该如何通过写代码的方式向其中添加“日志”呢? 在操作之前,先明确几个概念: 1:事件日志名(logName):“事件查看器”中的每一项,如“应用程序”.“Internet Explorer”.“ ...
- [Windows Server 2012] PHPWind安全设置
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★[护卫神·V课堂]是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:PHPWin ...
- Centos6.4 安装bind dns 服务器
一.介绍 1)Centos6.4 64bit minimal 2) bind-9.8.2-0.30.rc1.el6_6.3.x86_64 二.安装 $ yum install -y bind bind ...