-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbadlock5.cc
52 lines (49 loc) · 1.04 KB
/
badlock5.cc
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
#include <pthread.h>
class C {
public:
C() : val_(3) {
pthread_rwlockattr_t attr;
pthread_rwlockattr_init(&attr);
pthread_rwlockattr_setkind_np(
&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
pthread_rwlock_init(&mu_, &attr);
pthread_rwlockattr_destroy(&attr);
}
int calcSomething() {
pthread_rwlock_rdlock(&mu_);
int r = 42 + getVal();
pthread_rwlock_unlock(&mu_);
return r;
}
int getVal() {
pthread_rwlock_rdlock(&mu_);
int r = val_;
pthread_rwlock_unlock(&mu_);
return r;
}
void setVal(int val) {
pthread_rwlock_wrlock(&mu_);
val_ = val;
pthread_rwlock_unlock(&mu_);
}
private:
pthread_rwlock_t mu_;
int val_;
};
void* thread(void* data) {
C* c = (C*)data;
for (int i = 0; i < 100000; i++) {
c->calcSomething();
c->setVal(i);
}
return NULL;
}
int main() {
C* c = new C();
pthread_t th1;
pthread_create(&th1, NULL, &thread, c);
pthread_t th2;
pthread_create(&th2, NULL, &thread, c);
pthread_join(th1, NULL);
pthread_join(th2, NULL);
}