目录
1、读者和读者互不影响
2、写者和写者互斥
3、读者和写者互斥
(1) 读者持有锁
(2) 写者持有锁
假设现在只有读者线程,我们让一个读者线程申请锁以后,但是不释放读写锁。
#include
#include
#include pthread_rwlock_t rwlock;
void* read_thread(void* args){pthread_detach(pthread_self());int num = *(int*)args;while (1){pthread_rwlock_rdlock(&rwlock); // 申请读者锁printf("读者[%d]正在读内容\n", num);sleep(1);//pthread_rwlock_unlock(&rwlock); }
}int main(){pthread_t tid1,tid2,tid3,tid4;pthread_rwlock_init(&rwlock, NULL);int i = 1, j = 2;pthread_create(&tid1,NULL,read_thread, (void*)&i);pthread_create(&tid2,NULL,read_thread,(void*)&j);while(1){ sleep(1);} return 0;
}
我们发现,读者和读者之间不会去争抢锁,即便是某一个读者线程申请到锁,也不会影响其他读者线程来申请锁。
我们采用和上面类似的做法,只有两个写者线程,其中一个写者线程获取到锁以后,不释放锁,看一下另一个写者线程能否得到锁。
#include
#include
#include pthread_rwlock_t rwlock;
void* write_thread(void* args){pthread_detach(pthread_self());int num = *(int*)args;while (1){pthread_rwlock_wrlock(&rwlock);printf("写者[%d]正在写内容\n", num);//pthread_rwlock_unlock(&rwlock);sleep(1);}
}int main(){pthread_t tid1,tid2,tid3,tid4;pthread_rwlock_init(&rwlock, NULL);int i = 1, j = 2;pthread_create(&tid3,NULL,write_thread,(void*)&i);pthread_create(&tid4,NULL,write_thread,(void*)&j);while(1){ sleep(1);} return 0;
}
我们发现始终只有写者2在打印,说明写者2拿到锁以后,写者1就无法获取到锁了。说明写者和写者之间是互斥的。
这里就分为两种场景:读者持有锁、写者持有锁。
现在有三个线程,两个读线程,一个写线程,他们执行的操作如下:
#include
#include
#include pthread_rwlock_t rwlock;
void* read_thread1(void* args){pthread_detach(pthread_self());printf("读者[1]申请锁\n");while (1){pthread_rwlock_rdlock(&rwlock);printf("读者[1]正在读内容\n");sleep(1);// pthread_rwlock_unlock(&rwlock); // 读者1不释放锁}
}
void* read_thread2(void* args){pthread_detach(pthread_self());//int num = *(int*)args;sleep(2); // 让读者[2]延迟2s再申请printf("读者[2]申请锁\n");while (1){pthread_rwlock_rdlock(&rwlock);printf("读者[2]正在读内容\n");pthread_rwlock_unlock(&rwlock);sleep(1);}
}void* write_thread(void* args){pthread_detach(pthread_self());sleep(1); // 让写者[1]延迟1s再申请printf("写者1申请锁\n");while (1){pthread_rwlock_wrlock(&rwlock);printf("写者[1]正在写内容\n");pthread_rwlock_unlock(&rwlock);sleep(1);}
}int main(){pthread_t tid1,tid2,tid3,tid4;pthread_rwlock_init(&rwlock, NULL);int i = 1, j = 2;pthread_create(&tid1,NULL,read_thread1, NULL);pthread_create(&tid2,NULL,read_thread2, NULL);pthread_create(&tid3,NULL,write_thread, NULL);while(1){ sleep(1);} return 0;
}
我们采用和上面类似的步骤,
#include
#include
#include pthread_rwlock_t rwlock;
void* read_thread1(void* args){pthread_detach(pthread_self());sleep(1); // 让读者[1]延迟1s再申请printf("读者[1]申请锁\n");while (1){pthread_rwlock_rdlock(&rwlock);printf("读者[1]正在读内容\n");pthread_rwlock_unlock(&rwlock);sleep(1);}
}
void* read_thread2(void* args){pthread_detach(pthread_self());//int num = *(int*)args;sleep(1); // 让读者[2]延迟1s再申请printf("读者[2]申请锁\n");while (1){pthread_rwlock_rdlock(&rwlock);printf("读者[2]正在读内容\n");pthread_rwlock_unlock(&rwlock);sleep(1);}
}void* write_thread(void* args){pthread_detach(pthread_self());printf("写者1申请锁\n");while (1){pthread_rwlock_wrlock(&rwlock);printf("写者[1]正在读内容\n");// pthread_rwlock_unlock(&rwlock); // 写者1不释放锁sleep(1);}
}int main(){pthread_t tid1,tid2,tid3,tid4;pthread_rwlock_init(&rwlock, NULL);int i = 1, j = 2;pthread_create(&tid1,NULL,read_thread1, NULL);pthread_create(&tid2,NULL,read_thread2, NULL);pthread_create(&tid3,NULL,write_thread, NULL);while(1){ sleep(1);} return 0;
}