• 读写锁的简单示例


    /*使用读写锁实现四个线程读写一段程序的实例,共创建了四个新的线程,其中两个线程用来读取数据,另外两个线程用来写入数据。在任意时刻,如果有一个线程在写数据,将阻塞所有其他线程的任何操作。*/
    #include <errno.h>
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <bits/pthreadtypes.h>
    pthread_rwlock_t rwlock;//读写锁对象
    int a=0;
    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);
        }
      
        res = pthread_rwlock_destroy(&rwlock);//销毁读写锁               
        if (res != 0)
        {
           perror("rwlock destory failed");
           exit(EXIT_FAILURE);
        }
    }
    
    void *thread_function_read_o(void *arg)
    {   pthread_rwlock_rdlock(&rwlock);//获取读取锁
        printf("this is read thread1 get lock
    ");  
        sleep(10);
        printf("read thread1:%d
    ",a);
        pthread_rwlock_unlock(&rwlock);   
        pthread_exit(0);
    }
    
     void *thread_function_read_t(void *arg)
    {   pthread_rwlock_rdlock(&rwlock);
        printf("this is read thread2 get lock
    ");
        sleep(10);
        printf("read thread2:%d
    ",a);
        pthread_rwlock_unlock(&rwlock);   
        pthread_exit(0);
    }
    
    void *thread_function_write_o(void *arg)
    {
            pthread_rwlock_wrlock(&rwlock);//获取写入锁
            printf("this is write thread1 get lock
    ");
            sleep(5);
            a++;
            printf("write thread1:%d
    ",a);
            pthread_rwlock_unlock(&rwlock);//解锁
            pthread_exit(0);
    }
    
    void *thread_function_write_t(void *arg)
    {      
            pthread_rwlock_wrlock(&rwlock);//获取写入锁
            printf("this is write thread2 get lock
    ");
            a+=2;
            sleep(5);
            printf("write thread2:%d
    ",a);
            pthread_rwlock_unlock(&rwlock);//解锁
            pthread_exit(0);
    }
    程序中加的sleep仅仅是为了验证,同一时间只能有一个线程可以获取写入锁,但是可以有多个读者可以获取读取锁!
  • 相关阅读:
    HyperV应用指南之4虚拟机管理[转]
    Windows Server 2003文件夹不能共享的解决办法【转】
    彻底了解DVD:从入门到精通(二)[转]
    HyperV应用指南之2--安装HyperV Server 2008 R2并配置远程管理[转]
    HyperV应用指南之HyperV应用基础[转]
    IIS7.5由于权限不足而无法读取配置文件的解决办法
    C# 十六进制字符串与数值类型之间转换(转)
    分享一个Winform下的分页控件[转]
    mysql的replace函数替换字符串功能简介
    聊聊.net程序设计——浅谈使用VS2010建模拓展(下)[转]
  • 原文地址:https://www.cnblogs.com/leijiangtao/p/4150835.html
Copyright © 2020-2023  润新知