读写锁 pthread_rwlock

 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值

 

按 Ctrl+C 复制代码

读写锁

#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系统上使移植更加的容易,请不要忘记在适当的地方调用次函数,清晰的告诉系统我们使用的线程个数。虽然在某些系统上,这个调用是徒劳的,但是它的使用增强的移植性!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值