转自: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. HttpServletRequest中得到各种信息

    1.获得domain: StringBuffer url = request.getRequestURL(); String domain = url.delete(url.length() - re ...

  2. wxPython+Boa Constructor环境配置

    配置之前先完成eclipse + Pydev的配置环境.详见http://www.cnblogs.com/dflower/archive/2010/05/13/1734522.html 1. 安装 w ...

  3. TcxComboBox控件说明

    http://www.cnblogs.com/huangygdelphi/articles/2648490.html 属性: Text:ComboBox 的文本信息 EditText: 也是给Comb ...

  4. 12. javacript高级程序设计-DOM2和DOM3

    1. DOM2和DOM3 DOM2级规范定义了一些模块,用于增强DOM1级.“DOM2级核心”为不同的DOM类型引入了一些与XML命名空间有关的方法,这些变化只在使用XML或者XHTML,对于HTML ...

  5. TokuDB 引擎安装测试

    背景: TokuDB引擎是有Tokutek开发的一个数据库存储引擎,在设计之初便引入了独特的索引算法,在其官网测试的文章中看到TokuDB性能比InnoDB高出很多. MySQL是一个插件式的数据库, ...

  6. 用基础动画实现iOS控件循环旋转

    - (void)viewDidLoad { [super viewDidLoad]; UIButton* ag=[[UIButton alloc]initWithFrame:CGRectMake(sc ...

  7. JqueryUI学习笔记-自动完成autocomplete

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...

  8. android入门到熟练(五)---广播

    1.广播类型:标准广播和有序广播.标准广播是异步广播在广播发出之后所有接收器几乎会同一时刻接收到,没有先后顺序,效率高,但无法被截断.有序广播则是同步广播,同一时刻只能一个接收器接收这条消息,等执行完 ...

  9. 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  10. W3C对DOM2.0定义的标准事件

    DOM2.0模型将事件处理流程分为三个阶段: 一.事件捕获阶段, 二.事件目标阶段, 三.事件起泡阶段. 具体如图(图片来源于网络,侵删) 事件捕获:当某个元素触发某个事件(如onclick),顶层对 ...