main
test-branch
|
Last change
on this file since 1aaefd4 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago |
|
Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.
git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c
|
-
Property mode
set to
100644
|
|
File size:
834 bytes
|
| 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.