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