zoukankan      html  css  js  c++  java
  • 使用读写锁解决读者-写者问题

    读写锁

    读写锁适合于对数据结构的读次数比写次数多得多的情况.因为,读模式锁定时可以共享,以写 模式锁住时意味着独占,所以读写锁又叫共享-独占锁.

    初始化和销毁:

    #include <pthread.h>
    int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const
        pthread_rwlockattr_t *restrict attr);
    int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

    成功则返回0,出错则返回错误编号. 同互斥量以上,在释放读写锁占用的内存之前,需要先通过 pthread_rwlock_destroy对读写锁进行清理工作, 释放由init分配的资源.

    读和写:

    #include <pthread.h>
    int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
    int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
    int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

    成功则返回0,出错则返回错误编号.这3个函数分别实现获取读锁,获取写锁和释放锁的操作.获 取锁的两个函数是阻塞操作,同样,非阻塞的函数为:

    #include <pthread.h>
    int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
    int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);

    成功则返回0,出错则返回错误编号.非阻塞的获取锁操作,如果可以获取则返回0,否则返回 错误的EBUSY.

    代码:

    /*********    说明:
    *********    读写锁适合于对数据结构的读次数比写次数多得多的情况.因为,
    ********     读模式锁定时可以共享,以写 模式锁住时意味着独占,所以读写锁又叫共享-独占锁.
    ********     要让读者与写者之间、以及写者与写者之问要互斥地访同数据集;
    *********/
    
    #include <pthread.h>
    #include <signal.h>
    #include <stdio.h>
    //#include "apue.h"
    
    #define R 5 // reader NO.
    #define W 5 // reader and writer NO.
    
    pthread_rwlock_t lock; //it's mean writer can writing
    
    int readCount = 0;
    
    void* reader(void *arg)
    {
        int n = W;
        int id = *(int*)arg;
        while (n--)
        {
            sleep( rand() % 3 );
            pthread_rwlock_rdlock(&lock);
            printf("reader %d is reading
    ", id);
            sleep( rand() % 3 );
    
            pthread_rwlock_unlock(&lock);
            printf("reader %d is leaving
    ", id);
        }
        printf("----reader %d has done----
    ", id);
    }
    
    void* writer(void *arg)
    {
        int n = W;
        while (n--)
        {
            sleep( rand() % 3 );
            pthread_rwlock_wrlock(&lock);
            printf("	writer is writing
    ");
            sleep( rand() % 3 );
            pthread_rwlock_unlock(&lock);
            printf("	writer is leaving
    ");
        }
        printf("----writer has done----
    ");
    }
    
    int main(int argc, const char *argv[])
    {
        int err;
        pthread_t tid[R], writerTid;
        int i;
    
        err = pthread_create(&writerTid, NULL, writer, (void *)NULL);
        if (err != 0)
        {
            printf("can't create process for writer
    ");
        }
    
        pthread_rwlock_init(&lock, NULL);
        for (i = 0; i < R; i++)
        {
            err = pthread_create(&tid[i], NULL, reader, &i);
            if (err != 0)
            {
                printf("can't create process for reader");
            }
        }
        while(1);
        pthread_rwlock_destroy(&lock);
        return 0;
    }
  • 相关阅读:
    20.11.16 leetcode406 leetcode中的排序写法
    20.11.15 leetcode402
    20.11.14 leetcode1122(自定义排序)
    polyline NOIP模拟 数论 规律
    alien NOIP模拟 位运算 数论
    跳石头 vijos1981 NOIP2015 D2T1 二分答案 模拟 贪心
    寻找道路 vijos1909 NOIP2014 D2T2 SPFA
    不死的LYM NOIP模拟 二分+状压DP
    死亡的颂唱者 NOIP模拟 贪心 DFS
    无线网络发射器选址 vijos 1908 NOIP2014 D2T1 模拟
  • 原文地址:https://www.cnblogs.com/biglucky/p/4631582.html
Copyright © 2011-2022 走看看