main
test-branch
| Line | |
|---|
| 1 | #include <pthread.h>
|
|---|
| 2 |
|
|---|
| 3 | #define assert(e) { if(!(e)) { ERROR: goto ERROR; (void)0; } }
|
|---|
| 4 |
|
|---|
| 5 | int turn; // integer variable to hold the ID of the thread whose turn is it
|
|---|
| 6 | int x; // variable to test mutual exclusion
|
|---|
| 7 | int flag1 = 0, flag2 = 0; // boolean flags
|
|---|
| 8 |
|
|---|
| 9 | void* thr1(void* arg) { // frontend produces 12 transitions from this thread. It would be better if it would produce only 8!
|
|---|
| 10 | flag1 = 1;
|
|---|
| 11 | turn = 1;
|
|---|
| 12 | do {} while (flag2==1 && turn==1);
|
|---|
| 13 | // begin: critical section
|
|---|
| 14 | x = 0;
|
|---|
| 15 | assert(x<=0);
|
|---|
| 16 | // end: critical section
|
|---|
| 17 | flag1 = 0;
|
|---|
| 18 |
|
|---|
| 19 | return 0;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | void* thr2(void* arg) {
|
|---|
| 23 | flag2 = 1;
|
|---|
| 24 | turn = 0;
|
|---|
| 25 | do {} while (flag1==1 && turn==0);
|
|---|
| 26 | // begin: critical section
|
|---|
| 27 | x = 1;
|
|---|
| 28 | assert (x>=1);
|
|---|
| 29 | // end: critical section
|
|---|
| 30 | flag2 = 0;
|
|---|
| 31 |
|
|---|
| 32 | return 0;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | int main()
|
|---|
| 36 | {
|
|---|
| 37 | pthread_t t;
|
|---|
| 38 |
|
|---|
| 39 | pthread_create(&t, 0, thr1, 0);
|
|---|
| 40 | thr2(0);
|
|---|
| 41 |
|
|---|
| 42 | return 0;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.