3.11. 読み書きロック

1. 読み書きロックの概要

  • スレッドが既にミューテックスを保持している場合、ミューテックスはクリティカル セクションに入ろうとするすべてのスレッドをブロックします。ただし、現在ミューテックスを保持しているスレッドが共有リソースの読み取りとアクセスのみを希望し、他のいくつかのスレッドも同時に共有リソースの読み取りを希望している状況を考えてみてください。ただし、ミューテックスの排他性により、他のすべてのスレッドはロックを取得できない場合、共有リソースを読み取ったりアクセスしたりすることはできませんが、実際には複数のスレッドが同時に共有リソースを読み取ったりアクセスしたりしても問題はありません。
  • データの読み取り操作と書き込み操作では、データベース データの読み取りと書き込みのアプリケーションなど、読み取り操作が多く、書き込み操作が少なくなります。複数の読み取りを許可し、書き込みは 1 回だけ許可するという現在の要件を満たすために、スレッドは読み取り/書き込みロックを提供します。
  • 読み書きロックの特徴
    • 他のスレッドがデータを読み取る場合、他のスレッドは読み取り操作を実行できますが、書き込み操作は実行できません。
    • 他のスレッドがデータを書き込む場合、他のスレッドは操作の読み取りおよび書き込みを許可されません。
    • 書き込みは排他的、書き込み優先度は高い

2. 読み書きロック関連操作関数

  • 読み書きロックの種類pthread_rwlock_t
  • int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
  • int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
  • int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
  • int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
  • int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
  • int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
  • int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

3. 読み書きロックコードの例

#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

// 定义8个线程,3个写数据,5个读数据

// 定义全局变量
int num = 1;

// 定义读写锁
pthread_rwlock_t rwlock;

void * write(void * arg)
{
    
    
    while (1)
    {
    
    
        pthread_rwlock_wrlock(&rwlock);
        num ++ ;
        printf("+++write tid: %ld, num: %d\n", pthread_self(), num);
        pthread_rwlock_unlock(&rwlock);
        usleep(100);
    }
    pthread_exit(NULL);
}

void * read(void * arg)
{
    
    
    while (1)
    {
    
    
        pthread_rwlock_rdlock(&rwlock);
        printf("===read tid: %ld, num: %d\n", pthread_self(), num);
        pthread_rwlock_unlock(&rwlock);
        usleep(100);
    }
    pthread_exit(NULL);
}

int main()
{
    
    
    // 初始化读写锁
    pthread_rwlock_init(&rwlock, NULL);

    // 定义写的线程id
    pthread_t wtid[3], rtid[5];

    // 创建写线程
    for (int i = 0; i < 3; i ++ )
    {
    
    
        pthread_create(&wtid[i], NULL, write, NULL);
    }

    // 创建读线程
    for (int i = 0; i < 5; i ++ )
    {
    
    
        pthread_create(&rtid[i], NULL, read, NULL);
    }

    // 建议把线程分离换成pthread_join()操作

    // 设置写线程分离
    for (int i = 0; i < 3; i ++ )
    {
    
    
        pthread_detach(wtid[i]);
    }

    // 设置读线程分离
    for (int i = 0; i < 5; i ++ )
    {
    
    
        pthread_detach(rtid[i]);
    }

    // 释放读写锁
    // 但是子线程没结束的时候就释放锁可以吗?
    // 当所有进程没有持有锁的才能被释放
    // 学习中存在疑问
    pthread_rwlock_destroy(&rwlock);

    // 退出主线程
    pthread_exit(NULL);
    
    return 0;
}

4. 関連する機能が man ドキュメントに表示されない場合の対処方法

  1. 最初にコマンドを入力しますsudo apt-get update
    • 以下のいずれかの問題が発生した場合
错误:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic InRelease
  无法解析域名“mirrors.tuna.tsinghua.edu.cn”
错误:2 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-updates InRelease
  无法解析域名“mirrors.tuna.tsinghua.edu.cn”
错误:3 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-backports InRelease
  无法解析域名“mirrors.tuna.tsinghua.edu.cn”
错误:4 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-security InRelease
  无法解析域名“mirrors.tuna.tsinghua.edu.cn”
错误:5 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-proposed InRelease
  无法解析域名“mirrors.tuna.tsinghua.edu.cn”
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
所有软件包均为最新。
W: 无法下载 https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic/InRelease  无法解析域名“mirrors.tuna.tsinghua.edu.cn”
W: 无法下载 https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic-updates/InRelease  无法解析域名“mirrors.tuna.tsinghua.edu.cn”
W: 无法下载 https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic-backports/InRelease  无法解析域名“mirrors.tuna.tsinghua.edu.cn”
W: 无法下载 https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic-security/InRelease  无法解析域名“mirrors.tuna.tsinghua.edu.cn”
W: 无法下载 https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic-proposed/InRelease  无法解析域名“mirrors.tuna.tsinghua.edu.cn”
W: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件。
  • 解決
1. sudo vim /etc/resolv.conf # 添加如下内容
nameserver 8.8.8.8
nameserver 127.0.0.1
options edns0
 
2. 输入Esc,:wq,保存并退出
 
3. sudo /etc/init.d/networking restart
  1. 上記の問題を解決したら、コマンドを入力しますsudo apt-get install manpages-posix-dev

おすすめ

転載: blog.csdn.net/z2812470857/article/details/130101709