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]=='