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