1.23
2.0
main
test-branch
|
Last change
on this file since 50f834b was 609ffd1, checked in by John Edenhofner <johneden@…>, 12 years ago |
|
Added support for pthread join/exit interaction, new translations, updated documentation and other features
git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1080 fb995dde-84ed-4084-dfe6-e5aef3e2452c
|
-
Property mode
set to
100644
|
|
File size:
1.2 KB
|
| Rev | Line | |
|---|
| [609ffd1] | 1 | /* Testcase from Threader's distribution. For details see:
|
|---|
| 2 | http://www.model.in.tum.de/~popeea/research/threader
|
|---|
| 3 | */
|
|---|
| 4 | #include <civlc.h>
|
|---|
| 5 | #include "pthread.cvh"
|
|---|
| 6 | #define assert(e) if (!(e)) $assert($false);
|
|---|
| 7 |
|
|---|
| 8 | int flag1 = 0, flag2 = 0; // boolean flags
|
|---|
| 9 | int turn=0; // integer variable to hold the ID of the thread whose turn is it
|
|---|
| 10 | int x; // boolean variable to test mutual exclusion
|
|---|
| 11 | void *thr1(void *arg) {
|
|---|
| 12 | flag1 = 1;
|
|---|
| 13 | while (flag2 >= 1) {
|
|---|
| 14 | if (turn != 0) {
|
|---|
| 15 | flag1 = 0;
|
|---|
| 16 | while (turn != 0) {};
|
|---|
| 17 | flag1 = 1;
|
|---|
| 18 | }
|
|---|
| 19 | }
|
|---|
| 20 | // begin: critical section
|
|---|
| 21 | x = 0;
|
|---|
| 22 | assert(x<=0);
|
|---|
| 23 | // end: critical section
|
|---|
| 24 | turn = 1;
|
|---|
| 25 | flag1 = 0;
|
|---|
| 26 | return NULL; //Added this, should be here or pthread_exit should be called
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | void *thr2(void *arg) {
|
|---|
| 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 | return NULL; //Added this, should be here or pthread_exit should be called
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | int main(void) {
|
|---|
| 48 | pthread_t t1, t2;
|
|---|
| 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.