转自: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. 【Networking】flannel,pipework,weave,udp,vxlan,ovs等资料

    Add Open vSwitch-based multitenant backend for use with OpenShift / Kubernetes:  https://github.com/ ...

  2. How do I get ASP.NET Web API to return JSON instead of XML using Chrome

    public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Ro ...

  3. SQL Server 流程控制

    流程控制语句: BEGIN ... END WAITFOR GOTO WHILE IF ... ELSE BREAK RETURN CONTINURE   1.BEGIN ... END BEGIN ...

  4. iOS CLLocationManager 定位

    今天写个定位,本来很简单,但是在填写plist时,通过系统提示,只能看到NSLocationUsageDescription项目,根本不提示 (1)NSLocationAlwaysUsageDescr ...

  5. eclipse 工作环境配置

    1.更换编辑颜色, http://eclipse-color-theme.github.io/update/ 下载离线安装包,解压缩 eclipse-color-theme-update-site\u ...

  6. Android Volley入门到精通:初识Volley的基本用法

    1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行H ...

  7. 【leetcode】Linked List Cycle II (middle)

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  8. 【leetcode】Reverse Linked List(easy)

    Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...

  9. IOS - delegate为什么不使用retain

    循环引用所有的引用计数系统,都存在循环应用的问题.例如下面的引用关系: 对象a创建并引用了对象b.对象b创建并引用了对象c.对象c创建并引用了对象b. 这时候b和c的引用计数分别是2和1.当a不再使用 ...

  10. CString 操作

    CString Left( int nCount ) const;                   //从左边1开始获取前 nCount 个字符 CString Mid( int nFirst ) ...