source: CIVL/examples/pthread/threader/peterson_true.c@ e0fc189

1.23 2.0 main test-branch
Last change on this file since e0fc189 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: 905 bytes
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 flag1 = 0, flag2 = 0; // boolean flags
9int turn; // integer variable to hold the ID of the thread whose turn is it
10int x; // boolean variable to test mutual exclusion
11
12void * thr1(void * arg) {
13 flag1 = 1;
14 turn = 1;
15 while (flag2==1 && turn==1) {};
16 // begin: critical section
17 x = 0;
18 assert(x<=0);
19 // end: critical section
20 flag1 = 0;
21}
22
23void * thr2(void * arg) {
24 flag2 = 1;
25 turn = 0;
26 while (flag1==1 && turn==0) {};
27 // begin: critical section
28 x = 1;
29 assert(x>=1);
30 // end: critical section
31 flag2 = 0;
32}
33
34int main() {
35 pthread_t t1, t2;
36 pthread_create(&t1, 0, thr1, (void *)0);
37 pthread_create(&t2, 0, thr2, (void *)0);
38 pthread_join(t1, 0);
39 pthread_join(t2, 0);
40 return 0;
41}
Note: See TracBrowser for help on using the repository browser.