Skip to content
This repository was archived by the owner on Oct 3, 2021. It is now read-only.

Commit 72bef11

Browse files
added again szymanski and read_write_lock
1 parent 9af652e commit 72bef11

File tree

6 files changed

+1595
-0
lines changed

6 files changed

+1595
-0
lines changed

c/pthread-ext/17_szymanski.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
extern void abort(void);
2+
#include <assert.h>
3+
void reach_error() { assert(0); }
4+
5+
#include <pthread.h>
6+
7+
#undef assert
8+
#define assert(e) { if(!(e)) { ERROR: {reach_error();abort();}(void)0; } }
9+
10+
int flag1 = 0, flag2 = 0; // N integer flags
11+
int x; // variable to test mutual exclusion
12+
13+
void* thr1(void* arg) {
14+
while (1) {
15+
flag1 = 1;
16+
while (flag2 >= 3);
17+
flag1 = 3;
18+
if (flag2 == 1) {
19+
flag1 = 2;
20+
while (flag2 != 4);
21+
}
22+
flag1 = 4;
23+
while (flag2 >= 2);
24+
// begin critical section
25+
x = 0;
26+
assert(x<=0);
27+
// end critical section
28+
while (2 <= flag2 && flag2 <= 3);
29+
flag1 = 0;
30+
}
31+
32+
return 0;
33+
}
34+
35+
void* thr2(void* arg) {
36+
while (1) {
37+
flag2 = 1;
38+
while (flag1 >= 3);
39+
flag2 = 3;
40+
if (flag1 == 1) {
41+
flag2 = 2;
42+
while (flag1 != 4);
43+
}
44+
flag2 = 4;
45+
while (flag1 >= 2);
46+
// begin critical section
47+
x = 1;
48+
assert(x>=1);
49+
// end critical section
50+
while (2 <= flag1 && flag1 <= 3);
51+
flag2 = 0;
52+
}
53+
54+
return 0;
55+
}
56+
57+
int main()
58+
{
59+
pthread_t t;
60+
61+
pthread_create(&t, 0, thr1, 0);
62+
thr2(0);
63+
64+
return 0;
65+
}
66+

0 commit comments

Comments
 (0)