source: CIVL/examples/pthread/threader/lamport_true.c@ 83af34d

1.23 2.0 main test-branch
Last change on this file since 83af34d was e3151da, checked in by Ziqing Luo <ziqing@…>, 11 years ago

re-organized example directory

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1763 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.2 KB
Line 
1/* Testcase from Threader's distribution. For details see:
2 http://www.model.in.tum.de/~popeea/research/threader
3*/
4
5#include <pthread.h>
6#define assert(e) if (!(e)) ERROR: goto ERROR;
7
8int x=0, y=0;
9int b1=0, b2=0; // boolean flags
10int X; // boolean variable to test mutual exclusion
11
12void *thr1(void * arg) {
13 while (1) {
14 b1 = 1;
15 x = 1;
16 if (y != 0) {
17 b1 = 0;
18 while (y != 0) {};
19 continue;
20 }
21 y = 1;
22 if (x != 1) {
23 b1 = 0;
24 while (b2 >= 1) {};
25 if (y != 1) {
26 while (y != 0) {};
27 continue;
28 }
29 }
30 break;
31 }
32 // begin: critical section
33 X = 0;
34 assert(X <= 0);
35 // end: critical section
36 y = 0;
37 b1 = 0;
38}
39
40void *thr2(void * arg) {
41 while (1) {
42 b2 = 1;
43 x = 2;
44 if (y != 0) {
45 b2 = 0;
46 while (y != 0) {};
47 continue;
48 }
49 y = 2;
50 if (x != 2) {
51 b2 = 0;
52 while (b1 >= 1) {};
53 if (y != 2) {
54 while (y != 0) {};
55 continue;
56 }
57 }
58 break;
59 }
60 // begin: critical section
61 X = 1;
62 assert(X >= 1);
63 // end: critical section
64 y = 0;
65 b2 = 0;
66}
67
68int main() {
69 pthread_t t1, t2;
70 pthread_create(&t1, 0, thr1, 0);
71 pthread_create(&t2, 0, thr2, 0);
72 pthread_join(t1, 0);
73 pthread_join(t2, 0);
74 return 0;
75}
Note: See TracBrowser for help on using the repository browser.