#include <cstdio>#include<pthread.h>#include<unistd.h>intbuffer[Ten];inti,j;pthread_rwlock_t Rwlock;void*writefun (void*Arg) { intPID =pthread_self (); printf ("writer%d enters the waiting state .... \ n", PID); Pthread_rwlock_wrlock (&Rwlock); printf ("written by%d write >>>\n", PID); Sleep (Ten); printf ("written by%d write end \ n", PID); Pthread_rwlock_unlock (&Rwlock); returnNULL;}void*readfun (void*Arg) { intPID =pthread_self (); printf ("Reader%d enters the waiting state .... \ n", PID); Pthread_rwlock_rdlock (&Rwlock); printf ("Reader%d read <<<\n", PID); Sleep (1); printf ("Reader%d read end \ n", PID); Pthread_rwlock_unlock (&Rwlock); returnNULL;}intMain () {pthread_t pread[3],pwrite; Pthread_create (&pwrite,null,writefun,0); for(intI=0; I <3; i++) Pthread_create (&pread[i],null,readfun,0); Pthread_join (Pwrite,null); for(inti =0; I <3; i++) Pthread_join (pread[i],null); return 0;} Compilation option G+ +-lpthread-o main main.cpp./main
Linux multithreaded Programming-reader-writer issues