1.23
2.0
main
test-branch
| Line | |
|---|
| 1 | /**
|
|---|
| 2 | * Command line execution:
|
|---|
| 3 | * civl verify sync01_true.cvl
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | #include <civlc.h>
|
|---|
| 7 | #include "pthread.cvh"
|
|---|
| 8 |
|
|---|
| 9 | int num;
|
|---|
| 10 | pthread_mutex_t m;
|
|---|
| 11 | pthread_cond_t empty, full;
|
|---|
| 12 |
|
|---|
| 13 | void *thread1(void*arg)
|
|---|
| 14 | {
|
|---|
| 15 | pthread_mutex_lock(&m);
|
|---|
| 16 |
|
|---|
| 17 | while (num > 0)
|
|---|
| 18 | pthread_cond_wait(&empty, &m);
|
|---|
| 19 |
|
|---|
| 20 | num++;
|
|---|
| 21 |
|
|---|
| 22 | pthread_mutex_unlock(&m);
|
|---|
| 23 | pthread_cond_signal(&full);
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | void *thread2(void*arg)
|
|---|
| 28 | {
|
|---|
| 29 | pthread_mutex_lock(&m);
|
|---|
| 30 |
|
|---|
| 31 | while (num == 0)
|
|---|
| 32 | pthread_cond_wait(&full, &m);
|
|---|
| 33 |
|
|---|
| 34 | num--;
|
|---|
| 35 |
|
|---|
| 36 | pthread_mutex_unlock(&m);
|
|---|
| 37 |
|
|---|
| 38 | pthread_cond_signal(&empty);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | void main()
|
|---|
| 43 | {
|
|---|
| 44 | pthread_t t1, t2;
|
|---|
| 45 |
|
|---|
| 46 | num = 1;
|
|---|
| 47 |
|
|---|
| 48 | pthread_mutex_init(&m, 0);
|
|---|
| 49 | pthread_cond_init(&empty, 0);
|
|---|
| 50 | pthread_cond_init(&full, 0);
|
|---|
| 51 |
|
|---|
| 52 | pthread_create(&t1, 0, thread1, 0);
|
|---|
| 53 | pthread_create(&t2, 0, thread2, 0);
|
|---|
| 54 |
|
|---|
| 55 | pthread_join(t1, 0);
|
|---|
| 56 | pthread_join(t2, 0);
|
|---|
| 57 |
|
|---|
| 58 | if (num!=1)
|
|---|
| 59 | {
|
|---|
| 60 | $assert($false);
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.