1) 写者写时,不可读
2) 有别的读者正在读,可读
1) 有读者正在读,不可写
2) 有写者正在写,不可写
3) 无读者正在读,无写者正在写,可写
1) 有写者正在写或者等待写,须等到没有写者才能读
2) 没有写者,可以读
1) 写者与写者互斥。当其它写者正在写时,其它写者不能写。
2) 写者与读者互斥。之前只有读者在读,当写者出现时,必须等到之前的读者都读完才能写。这尊重了之前读者的意愿。
3) 写者可以有条件地插读者的队。当前有写者正写,有读者在等,这时来了新写者,新写者可以在那些读者之前执行。这不尊重
1 while(1) 2 { 3 pthread_mutex_lock(&writeLock); 4 {//临界区,限制只有一个写者修改数据 5 write(); 6 } 7 pthread_mutex_unlock(&writeLock); 8 }
1 while(1) 2 { 3 pthread_mutex_lock(&accessWriterCnt); 4 {//临界区,希望修改 writerCnt,独占 writerCnt 5 writerCnt++; 6 if(writerCnt == 1){ 7 //读者与写者互斥;使用readerLock来描述; 8 pthread_mutex_lock(&readerLock); 9 } 10 } 11 pthread_mutex_unlock(&accessWriterCnt); 12 13 14 pthread_mutex_lock(&writeLock); 15 {//临界区,限制只有一个写者修改数据 16 write(); 17 } 18 pthread_mutex_unlock(&writeLock); 19 20 pthread_mutex_lock(&accessWriterCnt); 21 {//临界区,希望修改 writerCnt,独占 writerCnt 22 writerCnt--; 23 if(writerCnt == 0){ 24 //阻止后续的读者加入待读队列 25 pthread_mutex_unlock(&readerLock); 26 } 27 } 28 pthread_mutex_unlock(&accessWriterCnt); 29 sleep(W_SLEEP); 30 }
1 while(1) 2 { 3 pthread_mutex_lock(&readerLock); 4 {//临界区 5 read(); 6 } 7 pthread_mutex_unlock(&readerLock); 8 }
1 while(1) 2 { 3 pthread_mutex_lock(&readerLock); 4 pthread_mutex_unlock(&readerLock); 5 read(); 6 }
1 while(1) 2 { 3 pthread_mutex_lock(&outerLock);//成千上万的读者被锁在这里 4 pthread_mutex_lock(&readerLock);//只被一个读者占有 5 6 pthread_mutex_unlock(&readerLock);//释放时,写者将优先获得readerLock 7 pthread_mutex_unlock(&outerLock); 8 read(); 9 }
1 pthread_mutex_lock(&writeLock); 2 {//临界区,限制只有一个写者修改数据 3 write(); 4 } 5 pthread_mutex_unlock(&writeLock);
1 while(1) 2 { 3 pthread_mutex_lock(&outerLock);//成千上万的读者被锁在这里 4 pthread_mutex_lock(&readerLock);//只被一个读者占有 5 6 pthread_mutex_unlock(&readerLock);//释放时,写者将优先获得readerLock 7 pthread_mutex_unlock(&outerLock); 8 9 pthread_mutex_lock(&writeLock);//每个执行到这里的读者都企图对 writeLock 加锁 10 {//临界区 11 read();//同一时刻只能有一个读者在这里,无法实现读并发 12 } 13 pthread_mutex_unlock(&writeLock); 14 }
1 pthread_mutex_lock(&accessReaderCnt); 2 {//临界区 3 readerCnt++; 4 if(readerCnt == 1){//第一个并发读线程 5 pthread_mutex_lock(&writeLock);//在第一个并发读者这里开始禁止写者执行写操作 6 } 7 } 8 pthread_mutex_unlock(&accessReaderCnt);
1 pthread_mutex_lock(&accessReaderCnt); 2 {//临界区 3 readerCnt--; 4 if(readerCnt == 0){//最后一个并发读线程 5 pthread_mutex_unlock(&writeLock);//在最后一个并发读者read()结束后写者可以执行写操作 6 } 7 } 8 pthread_mutex_unlock(&accessReaderCnt);
1 while(1) 2 { 3 4 //代码段1 可能的位置 0 5 pthread_mutex_lock(&outerLock);//假如写者锁定了readerLock,那么成千上万的读者被锁在这里 6 //代码段1 可能的位置 1 7 pthread_mutex_lock(&readerLock);//只被一个读者占有 8 //代码段1 可能的位置 2 9 pthread_mutex_unlock(&readerLock);//释放时,写者将优先获得readerLock 10 //代码段1 可能的位置 3 11 pthread_mutex_unlock(&outerLock); 12 //代码段1 可能的位置 4 13 14 read(); 15 pthread_mutex_lock(&accessReaderCnt);//代码段2 16 {//临界区 17 readerCnt--; 18 if(readerCnt == 0){ 19 pthread_mutex_unlock(&writeLock);//在最后一个并发读者读完这里开始禁止写者执行写操作 20 } 21 } 22 pthread_mutex_unlock(&accessReaderCnt); 23 }
1 while(1) 2 { 3 pthread_mutex_lock(&outerLock);//假如写者锁定了readerLock,那么成千上万的读者被锁在这里 4 {//临界区 5 pthread_mutex_lock(&readerLock);//只被一个读者占有 6 {//临界区 7 pthread_mutex_lock(&accessReaderCnt);//代码段 1 8 {//临界区 9 readerCnt++; 10 if(readerCnt == 1){ 11 pthread_mutex_lock(&writeLock); 12 } 13 } 14 pthread_mutex_unlock(&accessReaderCnt); 15 } 16 pthread_mutex_unlock(&readerLock);//释放时,写者将优先获得readerLock 17 } 18 pthread_mutex_unlock(&outerLock); 19 20 read(); 21 22 pthread_mutex_lock(&accessReaderCnt);//代码段2 23 {//临界区 24 readerCnt--; 25 if(readerCnt == 0){ 26 pthread_mutex_unlock(&writeLock);//在最后一个并发读者读完这里开始禁止写者执行写操作 27 } 28 } 29 pthread_mutex_unlock(&accessReaderCnt); 30 }
1 /* 2 * 多线程,读者优先 3 */ 4 5 #include "stdio.h" 6 #include7 #include 8 9 10 #define N_WRITER 3 //写者数目 11 #define N_READER 5 //读者数目 12 #define W_SLEEP 1 //控制写频率 13 #define R_SLEEP 1 //控制读频率 14 15 16 pthread_t wid[N_WRITER],rid[N_READER]; 17 const int MAX_RAND = 1000;//产生的最大随机数 18 pthread_mutex_t writeLock = PTHREAD_MUTEX_INITIALIZER;//同一时间只能一个人写文件,互斥 19 pthread_mutex_t accessReaderCnt = PTHREAD_MUTEX_INITIALIZER;//同一时间只能有一个人访问 readerCnt 20 int data = 0; 21 int readerCnt = 0; 22 void write() 23 { 24 int rd = rand(); 25 printf("write %d\n",rd); 26 data = rd; 27 } 28 void read() 29 { 30 printf("read %d\n",data); 31 } 32 void * writer(void * in) 33 { 34 while(1) 35 { 36 pthread_mutex_lock(&writeLock); 37 write(); 38 pthread_mutex_unlock(&writeLock); 39 sleep(W_SLEEP); 40 } 41 pthread_exit((void *) 0); 42 } 43 44 void * reader (void * in) 45 { 46 while(1) 47 { 48 pthread_mutex_lock(&accessReaderCnt); 49 readerCnt++; 50 if(readerCnt == 1){ 51 pthread_mutex_lock(&writeLock); 52 } 53 pthread_mutex_unlock(&accessReaderCnt); 54 55 read(); 56 57 pthread_mutex_lock(&accessReaderCnt); 58 readerCnt--; 59 if(readerCnt == 0){ 60 pthread_mutex_unlock(&writeLock); 61 } 62 pthread_mutex_unlock(&accessReaderCnt); 63 sleep(R_SLEEP); 64 } 65 pthread_exit((void *) 0); 66 } 67 68 int main() 69 { 70 int i = 0; 71 for(i = 0; i ) 72 { 73 pthread_create(&wid[i],NULL,reader,NULL); 74 } 75 for(i = 0; i ) 76 { 77 pthread_create(&rid[i],NULL,writer,NULL); 78 } 79 while(1){ 80 sleep(10); 81 } 82 return 0; 83 }
1 #include "stdio.h" 2 #include3 #include 4 5 6 #define N_WRITER 2 //写者数目 7 #define N_READER 20 //读者数目 8 #define W_SLEEP 1 //控制写频率 9 #define R_SLEEP 0.5 //控制读频率 10 11 12 pthread_t wid[N_WRITER],rid[N_READER]; 13 const int MAX_RAND = 1000;//产生的最大随机数 14 int data = 0; 15 int readerCnt = 0, writerCnt = 0; 16 pthread_mutex_t accessReaderCnt = PTHREAD_MUTEX_INITIALIZER; 17 pthread_mutex_t accessWriterCnt = PTHREAD_MUTEX_INITIALIZER; 18 pthread_mutex_t writeLock = PTHREAD_MUTEX_INITIALIZER; 19 pthread_mutex_t readerLock = PTHREAD_MUTEX_INITIALIZER; 20 pthread_mutex_t outerLock = PTHREAD_MUTEX_INITIALIZER; 21 22 void write() 23 { 24 int rd = rand()%MAX_RAND; 25 printf("write %d\n",rd); 26 data = rd; 27 } 28 void read() 29 { 30 printf("read %d\n",data); 31 } 32 void * writer(void * in) 33 { 34 while(1) 35 { 36 pthread_mutex_lock(&accessWriterCnt); 37 {//临界区,希望修改 writerCnt,独占 writerCnt 38 writerCnt++; 39 if(writerCnt == 1){ 40 //阻止后续的读者加入待读队列 41 pthread_mutex_lock(&readerLock); 42 } 43 } 44 pthread_mutex_unlock(&accessWriterCnt); 45 46 47 pthread_mutex_lock(&writeLock); 48 {//临界区,限制只有一个写者修改数据 49 write(); 50 } 51 pthread_mutex_unlock(&writeLock); 52 53 pthread_mutex_lock(&accessWriterCnt); 54 {//临界区,希望修改 writerCnt,独占 writerCnt 55 writerCnt--; 56 if(writerCnt == 0){ 57 //阻止后续的读者加入待读队列 58 pthread_mutex_unlock(&readerLock); 59 } 60 } 61 pthread_mutex_unlock(&accessWriterCnt); 62 sleep(W_SLEEP); 63 } 64 pthread_exit((void *) 0); 65 } 66 67 void * reader (void * in) 68 { 69 while(1) 70 { 71 //假如写者锁定了readerLock,那么成千上万的读者被锁在这里 72 pthread_mutex_lock(&outerLock); 73 {//临界区 74 pthread_mutex_lock(&readerLock);//只被一个读者占有 75 {//临界区 76 pthread_mutex_lock(&accessReaderCnt);//代码段 1 77 {//临界区 78 readerCnt++; 79 if(readerCnt == 1){ 80 pthread_mutex_lock(&writeLock); 81 } 82 } 83 pthread_mutex_unlock(&accessReaderCnt); 84 } 85 pthread_mutex_unlock(&readerLock);//释放时,写者将优先获得readerLock 86 } 87 pthread_mutex_unlock(&outerLock); 88 89 read(); 90 91 pthread_mutex_lock(&accessReaderCnt);//代码段2 92 {//临界区 93 readerCnt--; 94 if(readerCnt == 0){ 95 pthread_mutex_unlock(&writeLock);//在最后一个并发读者读完这里开始禁止写者执行写操作 96 } 97 } 98 pthread_mutex_unlock(&accessReaderCnt); 99 100 sleep(R_SLEEP); 101 } 102 pthread_exit((void *) 0); 103 } 104 105 int main() 106 { 107 int i = 0; 108 for(i = 0; i ) 109 { 110 pthread_create(&rid[i],NULL,reader,NULL); 111 } 112 for(i = 0; i ) 113 { 114 pthread_create(&wid[i],NULL,writer,NULL); 115 } 116 while(1){ 117 sleep(10); 118 } 119 return 0; 120 }