读写锁其实是一种特殊的自旋锁
在linux下有两种锁:
(1)自旋锁:不断地周而复始的做一件事情,不挂起,轮询
(2)挂起等待锁:信号量,互斥锁
读写锁也有三种关系: 写者和写者:互斥
读者和读者:没有关系,因为读者只读,不做其他操作,所以可以说是共享关系或者说没有关系
写者和读者:互斥,同步
读写锁的相关函数:
初始化读写锁 int pthread_rwlock_init (pthread_rwlcok_t *restrict rwlock) 参数是声明的读写锁变量
删除读写锁 int pthread_rwlock_destroy (pthread_rwlcok_t *rwlock)
加锁有两种函数 (1)阻塞式申请 int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)-----加只读锁
int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)-----加只写锁
(2)非阻塞式申请 int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)-----加只读锁
int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)-----加只写锁
释放锁 int pthread_rwlock_unlock(pthread_rwlock_t * rwlock)
测试代码:
1 #include
2 #include
3 int book;
4 pthread_rwlock_t rwlock;
5 void* read(void* arg)
6 {
7// sleep(1);//(2)
8 while(1)
9 {
10 if(pthread_rwlock_tryrdlock(&rwlock)!=0)
11 {
12 printf("writer is writing..!\n");
13 }else{
14 printf("read book is:%d\n",book);
15 // sleep(3);//(1)
16 pthread_rwlock_unlock(&rwlock);
17 }
18 }
19 }
20
21 void* write(void* arg)
22 {
23 // sleep(1);//(1)
24 while(1)
25 {
26 if(pthread_rwlock_trywrlock(&rwlock)!=0)
27 {
28 printf("reader is reading..!\n");
29 }else{
30 book++;
31 printf("write done:%d\n",book);
32// sleep(3);//(2)
33 pthread_rwlock_unlock(&rwlock);
34 }
35 }
36 }
37 int main()
38 {
39 pthread_rwlock_init(&rwlock,NULL);
40 pthread_t reader,writer;
41 pthread_create(&reader,NULL,read,NULL);
42 pthread_create(&writer,NULL,write,NULL);
43
44 pthread_join(reader,NULL);
45 pthread_join(writer,NULL);
46 pthread_rwlock_destroy(&rwlock);
47 return 0;
48 }
所以我们改变一下测试代码,观察两种情况
(1)读者可以读成功,sleep 3秒,但同时1秒后写者到达,但是写者没有写成功,因为读者在sleep,并没有释放锁
1 #include
2 #include
3 int book;
4 pthread_rwlock_t rwlock;
5 void* read(void* arg)
6 {
7// sleep(1);//(2)
8 while(1)
9 {
10 if(pthread_rwlock_tryrdlock(&rwlock)!=0)
11 {
12 printf("writer is writing..!\n");
13 }else{
14 printf("read book is:%d\n",book);
15 sleep(3);//(1)
16 pthread_rwlock_unlock(&rwlock);
17 }
18 }
19 }
20
21 void* write(void* arg)
22 {
23 sleep(1);//(1)
24 while(1)
25 {
26 if(pthread_rwlock_trywrlock(&rwlock)!=0)
27 {
28 printf("reader is reading..!\n");
29 }else{
30 book++;
31 printf("write done:%d\n",book);
32// sleep(3);//(2)
33 pthread_rwlock_unlock(&rwlock);
34 }
35 }
36 }
37 int main()
38 {
39 pthread_rwlock_init(&rwlock,NULL);
40 pthread_t reader,writer;
41 pthread_create(&reader,NULL,read,NULL);
42 pthread_create(&writer,NULL,write,NULL);
43
44 pthread_join(reader,NULL);
45 pthread_join(writer,NULL);
46 pthread_rwlock_destroy(&rwlock);
47 return 0;
48 }
(2)写者可以写成功,sleep 3秒,但同时1秒后读者到达,但是读者没有读成功,因为写者在sleep,并没有释放锁
1 #include
2 #include
3 int book;
4 pthread_rwlock_t rwlock;
5 void* read(void* arg)
6 {
7 sleep(1);//(2)
8 while(1)
9 {
10 if(pthread_rwlock_tryrdlock(&rwlock)!=0)
11 {
12 printf("writer is writing..!\n");
13 }else{
14 printf("read book is:%d\n",book);
15// sleep(3);//(1)
16 pthread_rwlock_unlock(&rwlock);
17 }
18 }
19 }
20
21 void* write(void* arg)
22 {
23// sleep(1);//(1)
24 while(1)
25 {
26 if(pthread_rwlock_trywrlock(&rwlock)!=0)
27 {
28 printf("reader is reading..!\n");
29 }else{
30 book++;
31 printf("write done:%d\n",book);
32 sleep(3);//(2)
33 pthread_rwlock_unlock(&rwlock);
34 }
35 }
36 }
37 int main()
38 {
39 pthread_rwlock_init(&rwlock,NULL);
40 pthread_t reader,writer;
41 pthread_create(&reader,NULL,read,NULL);
42 pthread_create(&writer,NULL,write,NULL);
43
44 pthread_join(reader,NULL);
45 pthread_join(writer,NULL);
46 pthread_rwlock_destroy(&rwlock);
47 return 0;
48 }