原文: 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. 【NOIP练习赛】学习

    [NOIP练习赛]T3.学习 Description 巨弱小 D 准备学习,有 n 份学习资料给他看,每份学习资料的 内容可以用一个正整数 ai 表示.小 D 如果在一天内学习了多份资料, 他只能记住 ...

  2. Linux命令(003) -- crontab

    一.准备知识 Linux下的任务调度分为两类:系统任务调度和用户任务调度. (1).系统任务调度 系统任务调度是系统周期性所要执行的工作,比如写缓存数据到硬盘.日志清理等.在/etc目录下有一个cro ...

  3. 兼容浏览器 div固定浏览器窗口底部 浮动div

    css内容: <style type="text/css"> #ken_BB { padding-right:30px; text-align: center; col ...

  4. EF 新增数据时提示it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element

    it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionM ...

  5. jvm gc日志解读

    参考 https://blog.csdn.net/yxc135/article/details/12137663 认识gc日志每个位置的含义 java 8 full gc [Full GC (Meta ...

  6. 重现apache commons fileupload DOS漏洞

    这个漏洞是2014年2月4日被发现的, 因为该组件试用范围非常广, 所以该漏洞的影响也非常巨大.通过特制的包含畸形header的http请求,可以导致使用该组件的应用程序进入无限循环从而耗尽CPU等资 ...

  7. Redis 之hash集合结构及命令详解

    1.hset key field value   作用: 把key中 filed域的值设为value 注:如果没有field域,直接添加,如果有,则覆盖原field域的值 2.hmset key fi ...

  8. win10系统安装postgresql后无法连接

    win10系统安装postgresql后在系统服务列表中找不到,连接不上数据库. 可以尝试关闭系统防火墙后重启电脑或者重装程序.

  9. 【Linq】

    " }; var l1 = strs.ToLookup(a => "a"); //Key=a elements=1,3 var l2 = strs.ToLookup ...

  10. C# 通知机制 IObserver<T> 和 IObservable<T>

    class Program { public static void Main() { // Define a provider and two observers. LocationTracker ...