**主要内容:
(1)互斥量及如何使用
(2)什么是死锁,如何解决
(3)什么是读写锁,如何使用**
(4)条件变量实现的生产消费者模型
(5)信号量实现的生产消费者模型
3.读写锁
特点:读共享,写独占,写优先级高。读写锁仍然是一把锁,有不同的状态:未加锁、读锁和写锁。
场景:适合读的线程较多的情况
- 线程A加写锁成功,线程B请求读锁。--线程B阻塞
- 线程A持有读锁,线程B请求写锁。--线程B阻塞
- 线程A持有读锁,线程B请求读锁。--B加锁成功
- 线程A持有读锁,然后线程B请求读锁,然后线程C请求写锁。--BC阻塞--A释放,C加锁--C释放B加锁
- 线程A持有写锁,然后线程B请求读锁,然后线程C请求写锁。--BC阻塞--A释放,C加锁--C释放B加锁
函数原型:#include <pthread.h>
(1)读写锁初始化
- int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,const pthread_rwlockattr_t *restrict attr);
- pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
(2)摧毁读写锁
- int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
(3)加读锁
-
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); //成功返回0,错误返回an error number shall be returned to indicate the error.
-
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock); //同上
(4)加写锁
- int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
(5)释放锁
- int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
示例:
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; //读写锁初始化
int beginnum = 1000;
void *thr_write(void *arg)
{
while (1)
{
pthread_rwlock_wrlock(&rwlock);
printf("------%s-----self----%lu---beginnum----%d\n",__FUNCTION__,pthread_self(),++beginnum);
usleep(2000); //模拟占用时间
pthread_rwlock_unlock(&rwlock);
usleep(4000);
}
return NULL;
}
void *thr_read(void *arg)
{
while (1)
{
pthread_rwlock_rdlock(&rwlock);
printf("------%s-----self----%lu---beginnum----%d\n",__FUNCTION__,pthread_self(),beginnum);
usleep(2000); //模拟占用时间
pthread_rwlock_unlock(&rwlock);
usleep(2000);
}
return NULL;
}
int main()
{
int n =8, i = 0;
pthread_t tid[8]; //5-read, 3-write
for(i = 0; i < 5; i++)
{
pthread_create(&tid[i],NULL,thr_read,NULL);
}
for(i = 0; i < 3; i++)
{
pthread_create(&tid[i],NULL,thr_write,NULL);
}
for(i=0; i < 8; i++)
{
pthread_join(tid[i],NULL);
}
return 0;
}
输出:
~$ gcc rwlock.c -lpthread
~$ ./a.out
------thr_read-----self----140403159697152---beginnum----1000
------thr_read-----self----140403151304448---beginnum----1000
------thr_read-----self----140403126126336---beginnum----1000
------thr_read-----self----140403134519040---beginnum----1000
------thr_read-----self----140403142911744---beginnum----1000
------thr_write-----self----140403117733632---beginnum----1001
------thr_write-----self----140403109340928---beginnum----1002
------thr_write-----self----140403100948224---beginnum----1003
------thr_write-----self----140403117733632---beginnum----1004
------thr_write-----self----140403109340928---beginnum----1005
------thr_write-----self----140403100948224---beginnum----1006
------thr_write-----self----140403117733632---beginnum----1007
------thr_write-----self----140403109340928---beginnum----1008
------thr_write-----self----140403100948224---beginnum----1009
------thr_write-----self----140403117733632---beginnum----1010
------thr_write-----self----140403109340928---beginnum----1011
------thr_write-----self----140403100948224---beginnum----1012
------thr_write-----self----140403117733632---beginnum----1013