原文: http://www.cnblogs.com/diegodu/p/3890450.html

使用读写锁

配置读写锁的属性之后,即可初始化读写锁。以下函数用于初始化或销毁读写锁、锁定或解除锁定读写锁或尝试锁定读写锁。下表列出了本节中讨论的用来处理读写锁的函数。

表 4–9 处理读写锁的例程

 

操作

相关函数说明

初始化读写锁

pthread_rwlock_init 语法

读取读写锁中的锁

pthread_rwlock_rdlock 语法

读取非阻塞读写锁中的锁

pthread_rwlock_tryrdlock 语法

写入读写锁中的锁

pthread_rwlock_wrlock 语法

写入非阻塞读写锁中的锁

pthread_rwlock_trywrlock 语法

解除锁定读写锁

pthread_rwlock_unlock 语法

销毁读写锁

pthread_rwlock_destroy 语法

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

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读写锁的更多相关文章

  1. 【C/C++多线程编程之九】pthread读写锁

    多线程编程之读写锁      Pthread是 POSIX threads 的简称,是POSIX的线程标准.         pthread读写锁把对共享资源的訪问者分为读者和写者,读者仅仅对共享资源 ...

  2. pthread 读写锁

    pthread 读写锁 (Read Write Lock, rwlock) 把对共享资源的访问者分为读者和写者,读者仅仅对共享资源进行读访问,写者仅仅对共享资源进行写操作. 如果使用互斥量 mutex ...

  3. c++ 读写锁

    #ifndef THREAD_UTIL_H #define THREAD_UTIL_H #include <pthread.h> namespace spider { class Auto ...

  4. pthread中读写锁

    读写锁很像一个互斥量,他阻止多个线程同时修改共享数据的另一种方法,区分不同互斥量的是他是分读数据和写数据,一个读写锁允许同时多个线程读数据,只要他们不修改数据. 只要没有写模式下的加锁,任意线程都可以 ...

  5. 基于pthread实现读写锁

    读写锁可用于在多线程访问map等数据结构时使用 #include <pthread.h> class ReadWriteLock { public: ReadWriteLock() { p ...

  6. 读写锁(pthread)

    读写锁: 用于对于某个给定资源的共享访问,而不是像互斥锁那样,将所有试图进入临界区的线程都阻塞住 相关内容: 线程互斥锁 分配规则:(写独占,读共享) 1.只要没有线程持有某个给定的读写锁用于写,那么 ...

  7. linux线程同步(3)-读写锁

    一.概述                                                    读写锁与互斥量的功能类似,对临界区的共享资源进行保护!互斥量一次只让一个线程进入临界区, ...

  8. linux读写锁

    一.概述                                                    读写锁与互斥量的功能类似,对临界区的共享资源进行保护!互斥量一次只让一个线程进入临界区, ...

  9. C基础 读写锁中级剖析

    引言 读写锁 是为了 解决, 大量 ''读'' 和 少量 ''写'' 的业务而设计的. 读写锁有3个特征: 1.当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞 2.当 ...

随机推荐

  1. 根据JSON创建对应的HIVE表

    本文提供一种用SCALA把JSON串转换为HIVE表的方法,由于比较简单,只贴代码,不做解释.有问题可以留言探讨 package com.gabry.hiveimport org.json4s._im ...

  2. ACM_“打老虎”的背后(简单并查集)

    “打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...

  3. Hadoop Hive概念学习系列之hive里的桶(十一)

    不多说,直接上干货!  Hive还可以把表或分区,组织成桶.将表或分区组织成桶有以下几个目的: 第一个目的是为看取样更高效,因为在处理大规模的数据集时,在开发.测试阶段将所有的数据全部处理一遍可能不太 ...

  4. java中使用String的replace方法替换html模板保存文件

    在我们的D盘下有这样一个html模板,现在我们要做的就是解析news.template文件,从数据库中提取数据将数据添加到指定的模板位置上 <head> <title>{tit ...

  5. 关于Adaper的相关用法

    使用BaseAdapter的话需要重载四个方法: getCount getItem getItemId getView getView是用来刷新它所在的ListView的.在每一次item从屏幕外滑进 ...

  6. ionic2 打包时报错 file-opener2

    在app自动更新过程中,有用到ionic-native插件:cordova-plugin-file-openner2    添加插件后,打包时有错: FAILURE: Build failed wit ...

  7. Angular——事件指令

    基本介绍 angular的事件指令都是ng-click,ng-blur....的形式,类似于js的事件 基本使用 <!DOCTYPE html> <html lang="e ...

  8. dotnetnuke 头像调用 头像缩放

    public static string GetProfileImage(int userId, int width, int height)        {                     ...

  9. Python语言之函数

    1.函数的定义与调用 def function(x): print("function(%s)"%x) function("hello") #call the ...

  10. python_时间日期

    time.time()用于获取当前时间戳 从返回浮点数的时间辍方式向时间元组转换,只要将浮点数传递给如localtime之类的函数. localtime = time.localtime(time.t ...