| | |
| | | #define NTHREADS 4 |
| | | struct Targ targs[NTHREADS]; |
| | | size_t qsize = 16; |
| | | |
| | | bool stop = false; |
| | | void sigint_handler(int sig) { |
| | | mm_destroy(); |
| | | exit(0); |
| | | |
| | | stop = true; |
| | | } |
| | | |
| | | |
| | |
| | | void* run (void *arg) { |
| | | struct Targ * targ = (struct Targ * )arg; |
| | | // SArrayLockFreeQueue<struct Item> *queue = QFactory::createArrayLockFreeQueue<struct Item> (targ->key, 10); |
| | | LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (targ->key, qsize); |
| | | // LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (targ->key, qsize); |
| | | SHMQueue<struct Item> *queue = new SHMQueue<struct Item>(targ->key, qsize); |
| | | |
| | | struct Item item; |
| | | struct timespec timeout = {5, 0}; |
| | | |
| | | int i = 0; |
| | | while((queue->pop_timeout(item, &timeout)) ) { |
| | | while(!stop && (queue->pop_timeout(item, &timeout)) ) { |
| | | printf("consumer(%d) 出队: {%d, %d}\n", targ->key, item.pic, item.info); |
| | | // cout << item.pic << endl; |
| | | |
| | | i++; |
| | | |
| | | } |
| | | delete queue; |
| | | return (void *)i; |
| | | } |
| | | |
| | |
| | | for (i = 0; i< NTHREADS; i++) { |
| | | targs[i].key = i; |
| | | pthread_create(&tids[i], NULL, run, (void *)&targs[i]); |
| | | sleep(1); |
| | | } |
| | | |
| | | for (i = 0; i< NTHREADS; i++) { |