/
rwlock.c
63 lines (52 loc) · 1.26 KB
/
rwlock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 3个线程不定时写同一全局资源, 5个线程不定时读同一全局资源
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>
int number = 0;
// 读写锁
pthread_rwlock_t lock;
void *write_func(void *arg) {
while (1) {
pthread_rwlock_wrlock(&lock);
number++;
printf("+++++write: %lu, %d\n", pthread_self(), number);
pthread_rwlock_unlock(&lock);
sleep(1);
}
return NULL;
}
void *read_func(void *arg) {
while (1) {
pthread_rwlock_rdlock(&lock);
printf("======read: %lu, %d\n", pthread_self(), number);
pthread_rwlock_unlock(&lock);
sleep(1);
}
return NULL;
}
int main() {
int i;
pthread_t p[8];
// 初始化读写锁
pthread_rwlock_init(&lock, NULL);
// 3个写线程
for (i = 0; i < 3; i++) {
pthread_create(&p[i], NULL, write_func, NULL);
}
// 5个读线程
for (i = 3; i < 8; i++) {
pthread_create(&p[i], NULL, read_func, NULL);
}
// 回收子线程
for (i = 0; i < 8; i++) {
pthread_join(p[i], NULL);
}
for (i = 0; i < 8; i++) {
pthread_rwlock_destroy(&lock);
}
return 0;
}