好好爱自己!

pthread_rwlock pthread读写锁

原文: 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值

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#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系统上使移植更加的容易,请不要忘记在适当的地方调用次函数,清晰的告诉系统我们使用的线程个数。虽然在某些系统上,这个调用是徒劳的,但是它的使用增强的移植性!

posted @   立志做一个好的程序员  阅读(1834)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2016-09-05 C# 开发系列(二)
2016-09-05 C# 开发系列(一)

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示