//Read Write lock#include <stdio.h>#include<unistd.h>#include<pthread.h>structtest{Chara[Ten]; Charb[Ten]; Charc[Ten];} YB= {"111","222","33333"};Static intj=0;p thread_rwlock_t mutex_1;voidPrint1 (structTest *Arg) {Pthread_rwlock_rdlock (&mutex_1); printf ("a=%s,b=%s,c=%s,d=%x,j=%d\n", Arg->a,arg->b,arg->c,pthread_self (), J + +); Sleep (2); Pthread_rwlock_unlock (&mutex_1); Pthread_exit ((void*) j); printf ("never coming.\n");}structTest *print2 (structTest *Arg) {Pthread_rwlock_wrlock (&mutex_1); structTest *p =NULL; P=Arg; memcpy (P->a,"FFF",3); memcpy (P->b,"GGG",3); memcpy (P->c,"TTT",3); Pthread_rwlock_unlock (&mutex_1); Pthread_exit ((void*)0);}intMain () {pthread_t pid1,pid2,pid3,pid4; void*Set; Pthread_rwlock_init (&mutex_1,null); Pthread_create (&pid1,NULL,&Print1,&YB); Sleep (1); Pthread_create (&pid2,NULL,&Print2,&YB); Pthread_create (&pid3,NULL,&Print1,&YB); Sleep (Ten); Pthread_create (&pid4,NULL,&Print1,&YB); Pthread_join (PID1,&Set); printf ("pid1 exit Coed%d\n",(int)Set); Pthread_join (Pid2,&Set); printf ("pid2 exit Coed%d\n",(int)Set); Pthread_join (PID3,&Set); printf ("pid3 exit Coed%d\n",(int)Set); Pthread_join (Pid4,&Set); printf ("pid4 exit Coed%d\n",(int)Set); Pthread_rwlock_destroy (&mutex_1); Sleep (1); return 0;}
Note 2 Linux multi-threaded read-write lock