读锁优先的实现

1、问题:在操作系统内对线程是写锁优先,如何实现读锁优先呢?

分析:的利用互斥量和条件变量来进行实现,由于已经对操作系统的写锁优先进行了实现,那么对其原理应该已经掌握了,直接代码实现。

代码如下:

(1)、utili.h

#include<unistd.h>
#include<stdio.h>
#include<pthread.h>

(2)、pthread_rwlock.h

#ifndef _PTHREAD_RWLOCK_H
#define _PTHREAD_RWLOCK_H

#include"utili.h"

enum {EINVAL, EBUSY};

typedef struct{
    pthread_mutex_t rw_mutex;
    pthread_cond_t  rw_condreaders;
    pthread_cond_t  rw_condwriters;
    int             rw_magic;
    int             rw_nwaitreaders;
    int             rw_nwaitwriters;
    int             rw_refcount;     //<0   ==0   >0
}my_pthread_rwlock_t;

#define RW_MAGIC 0x2016911

#define MY_PTHREAD_RWLOCK_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,                                     PTHREAD_COND_INITIALIZER,                                      PTHREAD_COND_INITIALIZER,                                     RW_MAGIC,                                     0, 0, 0}

typedef int my_pthread_rwlockattr_t;

void reader_clean_up(void *arg);
void writer_clean_up(void *arg);

int my_pthread_rwlock_init(my_pthread_rwlock_t *rw, my_pthread_rwlockattr_t *at);
int my_pthread_rwlock_destroy(my_pthread_rwlock_t *rw);
int my_pthread_rwlock_rdlock(my_pthread_rwlock_t *rw);
int my_pthread_rwlock_wrlock(my_pthread_rwlock_t *rw);
int my_pthread_rwlock_unlock(my_pthread_rwlock_t *rw);

int my_pthread_rwlock_tryrdlock(my_pthread_rwlock_t *rw);
int my_pthread_rwlock_trywrlock(my_pthread_rwlock_t *rw);

#endif

(3)、pthread_rwlock_init.c

#include"pthread_rwlock.h"

int my_pthread_rwlock_init(my_pthread_rwlock_t *rw, my_pthread_rwlockattr_t *at){
    int result;
    if(at != NULL)
        return EINVAL;

    if((result = pthread_mutex_init(&rw->rw_mutex, NULL)) != 0)
        goto err1;
    if((result = pthread_cond_init(&rw->rw_condreaders, NULL)) != 0)
        goto err2;
    if((result = pthread_cond_init(&rw->rw_condwriters, NULL)) != 0)
        goto err3;
    rw->rw_nwaitreaders = 0;
    rw->rw_nwaitwriters = 0;
    rw->rw_refcount = 0;
    rw->rw_magic = RW_MAGIC;

    return 0;

err3:
    pthread_cond_destroy(&rw->rw_condreaders);
err2:
    pthread_mutex_destroy(&rw->rw_mutex);
err1:
    return result;
}

(4)、pthread_rwlock_wrlock.c

#include"pthread_rwlock.h"

int my_pthread_rwlock_wrlock(my_pthread_rwlock_t *rw){
    int result;
    if(rw->rw_magic != RW_MAGIC)
        return EINVAL;
    if((result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
        return result;

    while(rw->rw_refcount!=0 || rw->rw_nwaitreaders>0){
        rw->rw_nwaitwriters++;
        result = pthread_cond_wait(&rw->rw_condwriters, &rw->rw_mutex);
        rw->rw_nwaitwriters--;
        if(result != 0){ 
            break;
        }   
    }   
    if(result == 0){ 
        rw->rw_refcount = -1; 
    }   

    pthread_mutex_unlock(&rw->rw_mutex);
    return result;
}

(5)、pthread_rwlock_rdlock.c

#include"pthread_rwlock.h"

int my_pthread_rwlock_rdlock(my_pthread_rwlock_t *rw){
    int result;
    if(rw->rw_magic != RW_MAGIC)
        return EINVAL;
    if((result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
        return result;

    //while(rw->rw_refcount<0 || rw->rw_nwaitwriters>0)
    while(rw->rw_refcount < 0){ 
        rw->rw_nwaitreaders++;
        //pthread_cleanup_push(reader_clean_up);
        result = pthread_cond_wait(&rw->rw_condreaders, &rw->rw_mutex);
        //pthread_cleanup_pop(0);
        rw->rw_nwaitreaders--;
        if(result != 0){ 
            break;
        }   
    }   
    if(result == 0){ 
        rw->rw_refcount++;
    }   

    pthread_mutex_unlock(&rw->rw_mutex);
    return result;
}

(6)、pthread_rwlock_trywrlock.c

#include"utili.h"
#include"pthread_rwlock.h"

int pthread_rwlock_trywrlock(my_pthread_rwlock_t *rw){
   int result;

   if(rw->rw_magic != RW_MAGIC){
        return EINVAL;
   }   

   if((result = pthread_mutex_lock(&rw->rw_mutex)) != 0){ 
       return result;
   }   
   if(rw->rw_refcount != 0){ 
        result = EBUSY;
   }else{
        rw->rw_refcount = -1; 
   }   

   pthread_mutex_unlock(&rw->rw_mutex);
   return result;
}

(7)、pthread_rwlock_tryrdlock.c

#include"utili.h"
#include"pthread_rwlock.h"

int pthread_rwlock_tryrdlock(my_pthread_rwlock_t *rw){
   int result;

   if(rw->rw_magic != RW_MAGIC){
        return EINVAL;
   }   

   if((result = pthread_mutex_lock(&rw->rw_mutex)) != 0){ 
       return result;
   }   
   if(rw->rw_refcount < 0 || rw->rw_nwaitwriters > 0){ 
        result = EBUSY;
   }else{
        rw->rw_refcount++;
   }   

   pthread_mutex_unlock(&rw->rw_mutex);
   return result;
}

(8)、pthread_rwlock_unlock.c

#include"pthread_rwlock.h"

int my_pthread_rwlock_unlock(my_pthread_rwlock_t *rw){
    int result;
    if(rw->rw_magic != RW_MAGIC)
        return EINVAL;
    if((result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
        return result;

    if(rw->rw_refcount > 0){ 
        rw->rw_refcount--;
    }else if(rw->rw_refcount == -1){
        rw->rw_refcount = 0;
    }else{
        printf("refcount error.\n");
    }   

    if(rw->rw_nwaitreaders > 0){ 
        result = pthread_cond_broadcast(&rw->rw_condreaders);
    }else if(rw->rw_nwaitwriters > 0){ 
        if(rw->rw_refcount == 0)
            result = pthread_cond_signal(&rw->rw_condwriters);
    }   
    //if(rw->rw_nwaitwriters > 0)
    //{ 
    //    if(rw->rw_refcount == 0)
    //        result = pthread_cond_signal(&rw->rw_condwriters);
    //}
    //else if(rw->rw_nwaitreaders > 0)
    //    result = pthread_cond_broadcast(&rw->rw_condreaders);

    pthread_mutex_unlock(&rw->rw_mutex);
    return result;
}

(9)、pthread_rwlock_destroy.c

#include"pthread_rwlock.h"

int my_pthread_rwlock_destroy(my_pthread_rwlock_t *rw){
    if(rw->rw_magic != RW_MAGIC)
        return EINVAL;
    if(rw->rw_refcount!=0 || rw->rw_nwaitreaders!=0 || rw->rw_nwaitwriters!=0)
        return EBUSY;
    pthread_mutex_destroy(&rw->rw_mutex);
    pthread_cond_destroy(&rw->rw_condreaders);
    pthread_cond_destroy(&rw->rw_condwriters);
    rw->rw_magic = 0;
    return 0;
}

(10)、test.c

#include"utili.h"
#include"pthread_rwlock.h"

my_pthread_rwlock_t rwlock = MY_PTHREAD_RWLOCK_INITIALIZER;

void* fun1(void *arg){
    my_pthread_rwlock_wrlock(&rwlock);
    printf("This is fun1. wrlock\n");
    sleep(5);
    printf("fun1 wake up.\n");
    my_pthread_rwlock_unlock(&rwlock);
}
void* fun2(void *arg){
    my_pthread_rwlock_rdlock(&rwlock);
    printf("This is fun2.rdlock.\n");
    my_pthread_rwlock_unlock(&rwlock);
}
void* fun3(void *arg){
    my_pthread_rwlock_wrlock(&rwlock);
    printf("This is fun3,wrlock.\n");
    my_pthread_rwlock_unlock(&rwlock);
}

int main(){
    pthread_t tid1, tid2[5],tid3[5];
    int i;
    pthread_create(&tid1, NULL, fun1, NULL);
    sleep(1);
    for(i=0; i<5; ++i)
    {
        pthread_create(&tid2[i], NULL, fun2, NULL);
    }

    for(i=0; i<5; ++i)
    {
        pthread_create(&tid3[i], NULL, fun3, NULL);
    }
    pthread_join(tid1, NULL);
    for(i=0; i<5; ++i)
    {
        pthread_join(tid2[i], NULL);
        pthread_join(tid3[i], NULL);
    }

    return 0;
}

(11)、Makefile

OBJ=test.o pthread_rwlock_rdlock.o pthread_rwlock_unlock.o pthread_rwlock_wrlock.o pthread_rwlock_init
.o
SRC=test.c pthread_rwlock_rdlock.c pthread_rwlock_unlock.c pthread_rwlock_wrlock.c pthread_rwlock_init
.c
test:$(OBJ)
    gcc -o test $(OBJ) -lpthread
test.o:test.c
    gcc -o test.o -c test.c -g
pthread_rwlock_rdlock.o:pthread_rwlock_rdlock.c
    gcc -o pthread_rwlock_rdlock.o -c pthread_rwlock_rdlock.c -g
pthread_rwlock_unlock.o:pthread_rwlock_unlock.c
    gcc -o pthread_rwlock_unlock.o -c pthread_rwlock_unlock.c -g
pthread_rwlock_wrlock.o:pthread_rwlock_wrlock.c
    gcc -o pthread_rwlock_wrlock.o -c pthread_rwlock_wrlock.c
pthread_rwlock_init.o:pthread_rwlock_init.c
    gcc -o pthread_rwlock_init.o -c pthread_rwlock_init.c
.PHONY:clean
clean:
    rm *.o test

运行结果

此时已经实现了读锁优先(只要对写锁优先熟悉,稍作更改就可以了)。

时间: 2024-08-09 04:37:33

读锁优先的实现的相关文章

Linux同步技术之读写锁

互斥锁试图将想进入临界区的所有线程都阻塞住,但是有时候该临界区会涉及由这些线程共享的一个或多个数据的访问或更新,这时候我们就需要用到读写锁. 系统读写锁的分配规则: (1)只要有没有线程持有给定的读写锁用于写,那么任意数量的线程可以持有该读写锁用于读.(系统规定写锁优先,但是可以更改为读锁优先) (2)仅当没有线程持有某个读写锁用于读或用于写时,才能分配该读写锁用于写. 读写锁用于读称为共享锁,读写锁用于写称为独占锁. 读写锁的获取与释放: int pthread_rwlock_rdlock(p

数据库的读锁和写锁在业务上的应用场景总结

一.背景 熟悉MySQL数据库的朋友们都知道,查询数据常见模式有三种: 1. select ... :快照读,不加锁 2. select ... in share mode:当前读,加读锁 3. select ... for update:当前读,加写锁 从技术层面理解三种方式的应用场景其实并不困难,下面我们先快速复习一下这三种读取模式的在技术层面上的区别. 注:为了简化问题的描述,下面所有结论均是针对MySQL数据库InnoDB储存引擎RR隔离级别下的. 1.1 select ... 读取当前

mysql 数据表读锁机制详解

为了给高并发情况下的mysql进行更好的优化,有必要了解一下mysql查询更新时的锁表机制.一.概述MySQL有三种锁的级别:页级.表级.行级.MyISAM和MEMORY存储引擎采用的是表级锁(table-level locking):BDB存储引擎采用的是页面锁(page-levellocking),但也支持表级锁:InnoDB存储引擎既支持行级锁(row-level locking),也支持表级锁,但默认情况下是采用行级锁.MySQL这3种锁的特性可大致归纳如下:表级锁:开销小,加锁快:不会

【Java并发】JUC—ReentrantReadWriteLock有坑,小心读锁!

载自:https://my.oschina.net/meandme/blog/1839265 好长一段时间前,某些场景需要JUC的读写锁,但在某个时刻内读写线程都报超时预警(长时间无响应),看起来像是锁竞争过程中出现死锁(我猜).经过排查项目并没有能造成死锁的可疑之处,因为业务代码并不复杂(仅仅是一个计算过程),经几番折腾,把注意力转移到JDK源码,正文详细说下ReentrantReadWriteLock的隐藏坑点. 过程大致如下: 若干个读写线程抢占读写锁 读线程手脚快,优先抢占到读锁(其中少

陈松松:制作视频优先选择这5种类型,总有一个适合你

这是我写的第88篇原创视频营销文章 陈松松,6年视频营销实战经验 万事开头难! 就看谁先能挺住! 很多朋友发现制作视频也有很多类型,不知道选择哪种最适合自己,今天我就跟大家详细分享下,当你学习之后,你根据自己的情况去选择和深入学习,也相当于给了你一个参考方向: 第一种:套视频模板 难度系数:三星 只需要修改文字.替换掉自己的图片,一般这种模板类的视频在10秒-2分钟左右,只要你的视频模板多,都可以制作出非常多的视频. 虽然套视频模板的视频非常简单,任何没有基础的人都可以操作,你用哪种视频软件的模

关于中缀表达式的计算 和算符优先算法

中缀表达式计算:http://blog.sina.com.cn/s/blog_3fe961ae0100niq3.html 算符优先算法:http://blog.csdn.net/zhibudefeng/article/details/6937375 前中后缀表达式:http://blog.csdn.net/antineutrino/article/details/6763722/

how2heap 1:优先原则

how2heap 1:优先原则-------------------------------------------示例代码#include <stdio.h>#include <stdlib.h>#include <string.h> int main(){    printf("This file doesn't demonstrate an attack, but shows the nature of glibc's allocator.\n"

优先双链表

题目: 设有一个双链表,每个结点中除有prior,data和 next这3个域外,还有一个访问频度域 freq,在链表被启用前其值均初始化为0.每当在在链表上进行一次查找操作Locate(L, x)时,令元素值为x的结点中的freq域的值增加1,并使此链表中的结点保持按访问频度域递减的顺序排列,以便使频繁访问的结点总是靠近表头 (1)首先创建一个双链表. (2) 设计一个符合上述要求的Locate(L, x)函数. (3) 具有输出显示访问频度功能. (4) 要求程序通过一个主菜单进行控制,在主

宽度优先搜索(BFS)

宽度优先搜索,又称为广度优先搜索,简称BFS 搜索过程:从初始结点开始,逐层向下扩展,即第n层搜索未完成,不得进入下一层搜索 一.初始结点入队,进入循环 二.取出队列的第一个元素 三.判断该结点是不是目标结点,如果是目标结点,则问题解决,跳出循环 四.如果该结点不是目标结点,判断其是否能够扩展,若不能,跳到步骤二 五.如果该结点能扩展,将其子结点放入队列的尾部 六.跳到步骤二 用一个经典的例子(走迷宫)来感受下 给定一个二维数组 int a[10][10] = {0 , 1 , 0 , 0 ,