zoukankan      html  css  js  c++  java
  • 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值

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <unistd.h>
     5 #include <pthread.h>
     6 #include <errno.h>
     7 
     8 #define MAXDATA     1024
     9 #define MAXREDER    100
    10 #define MAXWRITER   100
    11 struct
    12 {
    13     pthread_rwlock_t   rwlock; 
    14     char datas[MAXDATA];    
    15 } shared = { 
    16     PTHREAD_RWLOCK_INITIALIZER
    17 };
    18 
    19 void *reader(void *arg);
    20 void *writer(void *arg);
    21 
    22 int main(int argc,char *argv[])
    23 {
    24     int i,readercount,writercount;
    25     pthread_t tid_reader[MAXREDER],tid_writer[MAXWRITER];
    26     if(argc != 3)
    27     {   
    28         printf("usage : <reader_writer> #<readercount> #<writercount>
    ");
    29         exit(0);
    30     }   
    31     readercount = atoi(argv[1]);  
    32     writercount = atoi(argv[2]);
    33     pthread_setconcurrency(readercount+writercount);
    34     for(i=0;i<writercount;++i)
    35         pthread_create(&tid_writer[i],NULL,writer,NULL);
    36     sleep(1);
    37     for(i=0;i<readercount;++i)
    38         pthread_create(&tid_reader[i],NULL,reader,NULL);
    39     for(i=0;i<writercount;++i)
    40         pthread_join(tid_writer[i],NULL);
    41     for(i=0;i<readercount;++i)
    42         pthread_join(tid_reader[i],NULL);
    43     exit(0);
    44 }
    45 void *reader(void *arg)
    46 {
    47     pthread_rwlock_rdlock(&shared.rwlock);
    48     printf("Reader begins read message.
    ");
    49     sleep(1);
    50     printf("Read message is: %s
    ",shared.datas);
    51     pthread_rwlock_unlock(&shared.rwlock);
    52     return NULL;
    53 }
    54 
    55 void *writer(void *arg)
    56 {
    57     char datas[MAXDATA];
    58     pthread_rwlock_wrlock(&shared.rwlock);
    59     printf("Writers begings write message.
    ");
    60     printf("Enter the write message: 
    ");
    61     gets(datas);
    62     strcat(shared.datas,datas);
    63     pthread_rwlock_unlock(&shared.rwlock);
    64     return NULL;
    65 }

    打印输出

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

  • 相关阅读:
    [奇葩问题] Error Domain=NSURLErrorDomain Code=1003
    [linux] vim在源代码中自动添加作者信息(转载)
    [shell] 循环判断输入值
    [redis] linux下主从篇(2)
    [shell] sed学习
    [linux] 查看网卡UUID
    [笔记] centos6.6编译安装httpd2.4.10
    [笔记] postgresql 流复制(streaming replication)
    ORA28000: the account is locked 查哪个具体ip地址造成
    网易客户端授权密码,errormsg='authentication failed (method LOGIN)' exitcode=EX_NOPERM
  • 原文地址:https://www.cnblogs.com/diegodu/p/3890450.html
Copyright © 2011-2022 走看看