1、首先 读者写者的信号量实现
设置三个互斥信号量:
- rwmutex 用于写者与其他读者/写者互斥的访问共享数据
- rmutex 用于读者互斥的访问读者计数器readcount
- wmutex 用于写者等待已进入读者退出,所有读者退出前互斥写操作
-
var rwmutex, rmutex,wmutex : semaphore := 1,1,1 ; int readcount = 0; cobegin readeri begin // i=1,2,…. P(rwmutex); P(rmutex); Readcount++; If (readcount == 1) P(wmutex); //有读者进入,互斥写操作 V(rmutex); V(rwmutex); // 及时释放读写互斥信号量,允许其它读、写进程申请资源 读数据; P(rmutex); Readcount--; If (readcount == 0) V(wmutex); //所有读者退出,允许写更新 V(rmutex); End Writerj begin // j = 1,2,…. P(rwmutex); // 互斥后续其它读者、写者 P(wmutex); //如有读者正在读,等待所有读者读完 写更新; V(wmutex); //允许后续新的第一个读者进入后互斥写操作 V(rwmutex); //允许后续新读者及其它写者 End Coend
2 然后再linux平台下 我用到了 pthread_creat , pthread_mutex_trylock , pthread_mutex_unlock ;
代码实现,肯定还存在bug ,希望路过的同学批评指正。
#include <stdlib.h> #include <stdlib.h> /* for convenience */ #include <stddef.h> /* for offsetof */ #include <string.h> /* for convenience */ #include <unistd.h> /* for convenience */ #include <signal.h> /* for SIG_ERR */ #include <time.h> #include <pthread.h> pthread_mutex_t rwlock = PTHREAD_MUTEX_INITIALIZER; /*对缓冲访问的互斥*/ pthread_mutex_t rlock = PTHREAD_MUTEX_INITIALIZER; /*对readcount访问的互斥*/ pthread_mutex_t wlock = PTHREAD_MUTEX_INITIALIZER; /*write 线程 访问互斥*/ int readcount ; int readpos ,writepos ; const int N = 10; int pool[10]; pthread_t pthreadarray[10]; void print() { int i; printf("\n\n and now the pool is \n"); for(i = 0 ; i < N ; i ++) printf("%d is : %d\n ",i,pool[i]); sleep(2); } pthread_t find(pthread_t tid) { int i; for(i = 0 ; i < N ; i ++) if(pthreadarray[i] == tid) return i; return -1; } void * read_fn(void *arg) { pthread_t tid ; for(;;) { pthread_mutex_trylock(&rwlock); pthread_mutex_trylock(&rlock); readcount ++; if(readcount == 1) pthread_mutex_trylock(&wlock); tid = pthread_self(); printf("\n****************************************************************\n \ now is read thread reading and the number is %d ,tid is %ld\n \ the pool pos is %d and content is %d\n",find(tid),(long)tid,readpos,pool[readpos]); print(); pool[readpos] = -1; readpos = (readpos+1)%N; readcount --; pthread_mutex_unlock(&rlock); if(readcount == 0) pthread_mutex_unlock(&wlock); pthread_mutex_unlock(&rwlock); } } void * write_fn(void *arg) { int val; pthread_t tid; for(;;) { pthread_mutex_trylock(&rwlock); pthread_mutex_trylock(&wlock); val = rand(); tid = pthread_self(); printf("\n****************************************************************\n \ now is write thread writing and the number is %d ,tid is %ld\n \ the pool pos is %d and content is %d\n",find(tid),(long)tid,writepos,val); pool[writepos] = val; writepos = (writepos+1)%N; pthread_mutex_unlock(&wlock); pthread_mutex_unlock(&rwlock); print(); } } void init() { int i; for(i = 0 ; i < 10 ; i ++) pool[i] = -1; } int main() { init(); pthread_t thr1,thr2,thr3,thr4; int i = 0; srand((int)time(0)); /* pthread_create(&thr1,NULL,write_fn,NULL); printf("the thread is wirte and tid is %d\n",(int)thr1); pthread_create(&thr2,NULL,read_fn,NULL); printf("the thread is read and tid is %d\n",(int)thr2); pthread_create(&thr3,NULL,read_fn,NULL); printf("the thread is read and tid is %d\n",(int)thr3); pthread_create(&thr4,NULL,write_fn,NULL); printf("the thread is wirte and tid is %d\n",(int)thr4); */ for(i = 0 ; i < 10 ; i ++) { if(i%3 != 0) { pthread_create(&pthreadarray[i],NULL,write_fn,NULL); printf("start :the %d thread is wirte and tid is %ld\n",i,(long)pthreadarray[i]); } else { pthread_create(&pthreadarray[i],NULL,read_fn,NULL); printf("start :the %d thread is read and tid is %ld\n",i,(long)pthreadarray[i]); } } sleep(10); exit(0); }
参考:http://blog.csdn.net/lenic/article/details/4675142 我独立博客的地址 :http://www.fuxiang90.me/?p=314 欢迎访问交流