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