转自:http://blog.csdn.net/dsg333/article/details/22113489

/*使用读写锁实现四个线程读写一段程序的实例,共创建了四个新的线程,其中两个线程用来读取数据,另外两个线程用来写入数据。在任意时刻,如果有一个线程在写数据,将阻塞所有其他线程的任何操作。*/
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <bits/pthreadtypes.h>

static pthread_rwlock_t rwlock;//读写锁对象

#define WORK_SIZE 1024
char work_area[WORK_SIZE];
int time_to_exit;

void *thread_function_read_o(void *arg);//读线程1
void *thread_function_read_t(void *arg);//读线程2
void *thread_function_write_o(void *arg);//写线程1
void *thread_function_write_t(void *arg);//写线程2

int main(int argc,char *argv[])
{
    int res;
    pthread_t a_thread,b_thread,c_thread,d_thread;
    void *thread_result;

res=pthread_rwlock_init(&rwlock,NULL);//初始化读写锁
    if (res != 0)
    {
        perror("rwlock initialization failed");
        exit(EXIT_FAILURE);
    }
    res = pthread_create(&a_thread, NULL, thread_function_read_o, NULL);//create new thread创建线程
    if (res != 0)
    {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }

res = pthread_create(&b_thread, NULL, thread_function_read_t, NULL);//create new thread
    if (res != 0)
    {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }
    res = pthread_create(&c_thread, NULL, thread_function_write_o, NULL);//create new thread
    if (res != 0)
    {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }
    res = pthread_create(&d_thread, NULL, thread_function_write_t, NULL);//create new thread
    if (res != 0)
    {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }
   
     res = pthread_join(a_thread, &thread_result);//等待a_thread线程结束           
     if (res != 0)
     {
         perror("Thread join failed");
         exit(EXIT_FAILURE);
     }
     res = pthread_join(b_thread, &thread_result);           
    if (res != 0)
     {
         perror("Thread join failed");
         exit(EXIT_FAILURE);
     }
    res = pthread_join(c_thread, &thread_result);           
    if (res != 0)
    {
        perror("Thread join failed");
        exit(EXIT_FAILURE);
    }
    res = pthread_join(d_thread, &thread_result);           
    if (res != 0)
    {
       perror("Thread join failed");
       exit(EXIT_FAILURE);
    }
  
    pthread_rwlock_destroy(&rwlock);//销毁读写锁               
    exit(EXIT_SUCCESS);
}

void *thread_function_read_o(void *arg)
{
    printf("thread read one try to get lock\n");   
   
    pthread_rwlock_rdlock(&rwlock);//获取读取锁
    while(strncmp("end", work_area, 3) != 0)
    {
        printf("this is thread read one.");
        printf("the characters is %s",work_area);   
        pthread_rwlock_unlock(&rwlock);           
        sleep(2);
        pthread_rwlock_rdlock(&rwlock);       
        while (work_area[0] == '\0' )          
        {
            pthread_rwlock_unlock(&rwlock);   
            sleep(2);
            pthread_rwlock_rdlock(&rwlock);
        }
    }   
    pthread_rwlock_unlock(&rwlock);   
    time_to_exit=1;
    pthread_exit(0);
}

void *thread_function_read_t(void *arg)
{
    printf("thread read one try to get lock\n");
    pthread_rwlock_rdlock(&rwlock);
    while(strncmp("end", work_area, 3) != 0)
    {
        printf("this is thread read two.");
        printf("the characters is %s",work_area);   
        pthread_rwlock_unlock(&rwlock);           
        sleep(5);
        pthread_rwlock_rdlock(&rwlock);           
        while (work_area[0] == '\0' )          
        {               
            pthread_rwlock_unlock(&rwlock);   
            sleep(5);
            pthread_rwlock_rdlock(&rwlock);   
        }
    }
    pthread_rwlock_unlock(&rwlock);   
    time_to_exit=1;
    pthread_exit(0);
}

void *thread_function_write_o(void *arg)
{
    printf("this is write thread one try to get lock\n");
    while(!time_to_exit)
    {
        pthread_rwlock_wrlock(&rwlock);
        printf("this is write thread one.\nInput some text. Enter 'end' to finish\n");
        fgets(work_area, WORK_SIZE, stdin);
        pthread_rwlock_unlock(&rwlock);
        sleep(15);
    }
    pthread_rwlock_unlock(&rwlock);
    pthread_exit(0);
}

void *thread_function_write_t(void *arg)
{
    sleep(10);
    while(!time_to_exit)
    {
        pthread_rwlock_wrlock(&rwlock);//获取写入锁
        printf("this is write thread two.\nInput some text. Enter 'end' to finish\n");
        fgets(work_area, WORK_SIZE, stdin);//写入
        pthread_rwlock_unlock(&rwlock);//解锁
        sleep(20);
    }
    pthread_rwlock_unlock(&rwlock);//解锁
    pthread_exit(0);
}

linux 读写锁应用实例的更多相关文章

  1. linux读写锁

    一.概述                                                    读写锁与互斥量的功能类似,对临界区的共享资源进行保护!互斥量一次只让一个线程进入临界区, ...

  2. Linux读写锁的使用

    读写锁是用来解决读者写者问题的,读操作可以共享,写操作是排它的,读可以有多个在读,写只有唯一个在写,写的时候不允许读. 具有强读者同步和强写者同步两种形式: 强读者同步:当写者没有进行写操作时,读者就 ...

  3. Linux 读写锁

    线程的读写锁函数: 1,读写锁的初始化与销毁,静态初始化的话,可以直接使用PTHREAD_RWLOCK_INITIALIZER. #include <pthread.h> int pthr ...

  4. linux线程间同步(1)读写锁

    读写锁比mutex有更高的适用性,能够多个线程同一时候占用读模式的读写锁.可是仅仅能一个线程占用写模式的读写锁. 1. 当读写锁是写加锁状态时,在这个锁被解锁之前,全部试图对这个锁加锁的线程都会被堵塞 ...

  5. linux c 线程间同步(通信)的几种方法--互斥锁,条件变量,信号量,读写锁

    Linux下提供了多种方式来处理线程同步,最常用的是互斥锁.条件变量.信号量和读写锁. 下面是思维导图:  一.互斥锁(mutex)  锁机制是同一时刻只允许一个线程执行一个关键部分的代码. 1 . ...

  6. 读写锁ReadWriteLock和缓存实例

    读写锁:多个读锁不互斥,读锁与写锁互斥,写锁与写锁互斥.即:读的时候不允许写,写的时候不允许读,可以同时读.      synchronized关键字和普通的Lock构造的锁,会造成读与读之间的互斥, ...

  7. Linux的线程同步对象:互斥量Mutex,读写锁,条件变量

        进程是Linux资源分配的对象,Linux会为进程分配虚拟内存(4G)和文件句柄等 资源,是一个静态的概念.线程是CPU调度的对象,是一个动态的概念.一个进程之中至少包含有一个或者多个线程.这 ...

  8. linux中读写锁的rwlock介绍-nk_ysg-ChinaUnix博客

    linux中读写锁的rwlock介绍-nk_ysg-ChinaUnix博客 linux中读写锁的rwlock介绍 2013-02-26 13:59:35 分类: C/C++   http://yaro ...

  9. linux 内核的另一个自旋锁 - 读写锁

    除spinlock外,linux 内核还有一个自旋锁,名为arch_rwlock_t.它的头文件是qrwlock.h,包含在spinlock.h,头文件中对它全称为"Queue read/w ...

随机推荐

  1. 【Java】嵌套For循环性能优化案例

    参考资料:http://cgs1999.iteye.com/blog/1596671 1 案例描述 某日,在JavaEye上看到一道面试题,题目是这样的:请对以下的代码进行优化 for (int i  ...

  2. 【GoLang】GoLang 错误处理 -- 异常处理思路示例

    代码: package main import ( "fmt" // "testing" ) var Pkg = "packageName" ...

  3. 【GoLang】GO语言系列--002.GO语言基础

    002.GO语言基础 1 参考资料 1.1 http://www.cnblogs.com/vimsk/archive/2012/11/03/2736179.html 1.2 https://githu ...

  4. 将数据导入带模板EXCEL

    在EXCEL模板里设置好样式和格式 点击事件 private void btnReport_Click(object sender, EventArgs e)        {            ...

  5. 【leetcode】Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  6. POJ 1979

    这是一道比较水的DPS的题目 题意就是求你可以走到的黑色的地板砖的块数,@代表你的起点,也是黑色的地板砖,#代表白色的,则说明你不能走,这就是一个广搜的题目 思路也很简单,如果你周围的那块地板是黑色的 ...

  7. 使用phpmyadmin修改XAMPP中MySQL的默认空密码

    XAMPP是开发php应用的一套完整的工具合集,就像安装软件一样安装,其他的都配置好了,不用自己再去繁琐的单独配置Apache.MySQL.php这几个模块了,以前我一直在使用的是Appserv,也是 ...

  8. win8内置管理员用户无法激活此应用

    在运行中输入:“gpedit.msc”,就会启动组策略编辑器, 计算机配置 --> Windows设置 --> 安全设置 --> 本地策略 -->  安全选项  ::::  用 ...

  9. Pythonj~module

    常数,变量 特殊变量:__xxx__  可以被直接引用 private函数:_xxx __xxx__ 外部不需要引用的函数全部定义成private,只有外部需要引用的函数才定义为public. 导入 ...

  10. 一道常考fork题挖掘

    #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { int ...