首页 > 代码库 > 多线程读写锁机制

多线程读写锁机制

  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\n"); 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\n",work_area); 57         pthread_rwlock_unlock(&rwlock); 58         sleep(2); 59         pthread_rwlock_rdlock(&rwlock); 60         while(work_area[0]==\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\n"); 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\n",work_area); 79         pthread_rwlock_unlock(&rwlock); 80         sleep(5); 81         pthread_rwlock_rdlock(&rwlock); 82         while(work_area[0]==\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\n"); 98     while(!time_to_exit) 99     {100         pthread_rwlock_wrlock(&rwlock);101         printf("this is write thread one.\nInput some text. Enter ‘end‘ to finish\n");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\n");113     while(!time_to_exit)114     {115         pthread_rwlock_wrlock(&rwlock);116         printf("this is write thread two.\nInput some text. Enter ‘end‘ to finish\n");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 }