| 1 | #include "queue.h" |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | |
| 5 | #include <pthread.h> |
| 6 | #include <time.h> |
| 7 | |
| 8 | #define MAX_NUMS 100000 |
| 9 | |
| 10 | void* producer(void* arg) |
| 11 | { |
| 12 | queue_t* queue = (queue_t*)arg; |
| 13 | size_t i = 0; |
| 14 | for(i = 0; i < MAX_NUMS; ++i) |
| 15 | { |
| 16 | queue_add(queue, i); |
| 17 | } |
| 18 | return NULL; |
| 19 | } |
| 20 | |
| 21 | void* consumer(void* arg) |
| 22 | { |
| 23 | queue_t* queue = (queue_t*)arg; |
| 24 | size_t i = 0; |
| 25 | for(i = 0; i < MAX_NUMS; ++i) |
| 26 | { |
| 27 | printf("got %d from producer\n", queue_pop(queue)); |
| 28 | } |
| 29 | return NULL; |
| 30 | } |
| 31 | |
| 32 | int main(int argc, char* argv[]) |
| 33 | { |
| 34 | pthread_t prod_thr, cons_thr; |
| 35 | pthread_cond_t valid_root; |
| 36 | pthread_mutex_t mx_cond; |
| 37 | queue_t myqueue; |
| 38 | queue_init(&myqueue); |
| 39 | |
| 40 | pthread_create(&cons_thr, NULL, producer, &myqueue); |
| 41 | pthread_create(&prod_thr, NULL, consumer, &myqueue); |
| 42 | |
| 43 | pthread_exit(0x0); |
| 44 | queue_clear(&myqueue); |
| 45 | return 0; |
| 46 | } |
| 47 | |