• 多线程读写锁机制


      1 #include <stdio.h>
      2 #include <pthread.h>
      3 #include <stdio.h>
      4 #include <errno.h>
      5 #include <stdlib.h>
      6 static pthread_rwlock_t rwlock;
      7 #define WORK_SIZE 1024
      8 char work_area[WORK_SIZE];
      9 int time_to_exit=0;
     10 
     11 void *thread_function_read_o(void *arg);
     12 void *thread_function_read_t(void *arg);
     13 void *thread_function_write_o(void *arg);
     14 void *thread_function_write_t(void *arg);
     15 
     16 int main(int argc, char *argv[])
     17 {
     18     int res;
     19     pthread_t a_thread,b_thread,c_thread,d_thread;
     20     void *thread_result;
     21     res=pthread_rwlock_init(&rwlock,NULL);
     22     if(res!=0)
     23         exit(EXIT_FAILURE);
     24     res=pthread_create(&a_thread,NULL,thread_function_read_o,NULL);
     25     if(res!=0)
     26         exit(EXIT_FAILURE);
     27     res=pthread_create(&b_thread,NULL,thread_function_read_t,NULL);
     28     if(res!=0)
     29         exit(EXIT_FAILURE);
     30     res=pthread_create(&c_thread,NULL,thread_function_write_o,NULL);
     31     if(res!=0)
     32         exit(EXIT_FAILURE);
     33     res=pthread_create(&d_thread,NULL,thread_function_write_t,NULL);
     34     if(res!=0)
     35         exit(EXIT_FAILURE);
     36     res=pthread_join(a_thread,&thread_result);
     37     if(res!=0)
     38         exit(EXIT_FAILURE);
     39     res=pthread_join(b_thread,&thread_result);
     40     if(res!=0)
     41         exit(EXIT_FAILURE);
     42     res=pthread_join(c_thread,&thread_result);
     43     if(res!=0)
     44         exit(EXIT_FAILURE);
     45     res=pthread_join(d_thread,&thread_result);
     46     pthread_rwlock_destroy(&rwlock);
     47     return 0;
     48 }
     49 void *thread_function_read_o(void *arg)
     50 {
     51     printf("thread read one try to get lock
    ");
     52     pthread_rwlock_rdlock(&rwlock);
     53     while(strncmp("end",work_area,3)!=0)
     54     {
     55         printf("this is thread read one.");
     56         printf("the characters is %s
    ",work_area);
     57         pthread_rwlock_unlock(&rwlock);
     58         sleep(2);
     59         pthread_rwlock_rdlock(&rwlock);
     60         while(work_area[0]=='')
     61         {
     62             pthread_rwlock_unlock(&rwlock);
     63             sleep(2);
     64             pthread_rwlock_rdlock(&rwlock);
     65         }
     66     }
     67     pthread_rwlock_unlock(&rwlock);
     68     time_to_exit=1;
     69     pthread_exit(0);
     70 }
     71 void *thread_function_read_t(void *arg)
     72 {
     73     printf("thread read two try to get lock
    ");
     74     pthread_rwlock_rdlock(&rwlock);
     75     while(strncmp("end",work_area,3)!=0)
     76     {
     77         printf("this is thread read two.");
     78         printf("the characters is %s
    ",work_area);
     79         pthread_rwlock_unlock(&rwlock);
     80         sleep(5);
     81         pthread_rwlock_rdlock(&rwlock);
     82         while(work_area[0]=='')
     83         {
     84             pthread_rwlock_unlock(&rwlock);
     85             sleep(5);
     86             pthread_rwlock_rdlock(&rwlock);
     87         }
     88 
     89     }
     90     pthread_rwlock_unlock(&rwlock);
     91     time_to_exit=1;
     92     pthread_exit(0);
     93 }
     94 
     95 void *thread_function_write_o(void *arg)
     96 {
     97     printf("this is write thread one try to get lock
    ");
     98     while(!time_to_exit)
     99     {
    100         pthread_rwlock_wrlock(&rwlock);
    101         printf("this is write thread one.
    Input some text. Enter 'end' to finish
    ");
    102         fgets(work_area,WORK_SIZE,stdin);
    103         pthread_rwlock_unlock(&rwlock);
    104         sleep(15);
    105     }
    106     pthread_rwlock_unlock(&rwlock);
    107     pthread_exit(0);
    108 }
    109 
    110 void *thread_function_write_t(void *arg)
    111 {
    112     printf("this is write thread two try to get lock
    ");
    113     while(!time_to_exit)
    114     {
    115         pthread_rwlock_wrlock(&rwlock);
    116         printf("this is write thread two.
    Input some text. Enter 'end' to finish
    ");
    117         fgets(work_area,WORK_SIZE,stdin);
    118         pthread_rwlock_unlock(&rwlock);
    119         sleep(20);
    120     }
    121     pthread_rwlock_unlock(&rwlock);
    122     pthread_exit(0);
    123 }
  • 相关阅读:
    基于python创建一个简单的HTTP-WEB服务器
    基于python自动化测试平台与虚拟化技术结合的思考
    Net分布式系统之六:微服务之API网关
    Net分布式系统之五:C#使用Redis集群缓存
    Net分布式系统之二:CentOS系统搭建Nginx负载均衡
    Net分布式系统之一:系统整体框架介绍
    .Net微服务架构之运行日志分析系统
    Net分布式系统之七:日志采集系统(1)
    程序员如何选择未来的职业路线
    NET技术公众号已上线
  • 原文地址:https://www.cnblogs.com/lakeone/p/3790594.html
Copyright © 2020-2023  润新知