File was renamed from test/consumer.c |
| | |
| | | using namespace std; |
| | | |
| | | |
| | | #define NTHREADS 4 |
| | | struct Targ targs[NTHREADS]; |
| | | size_t qsize = 16; |
| | | |
| | | void sigint_handler(int sig) { |
| | | destroy(); |
| | | mm_destroy(); |
| | | exit(0); |
| | | |
| | | } |
| | |
| | | void* run (void *arg) { |
| | | struct Targ * targ = (struct Targ * )arg; |
| | | // SArrayLockFreeQueue<struct Item> *queue = QFactory::createArrayLockFreeQueue<struct Item> (targ->key, 10); |
| | | SLinkedLockFreeQueue<struct Item> *queue = QFactory::createLinkedLockFreeQueue<struct Item> (targ->key, 10); |
| | | |
| | | LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (targ->key, qsize); |
| | | struct Item item; |
| | | struct timespec timeout = {5, 0}; |
| | | |
| | | int i = 0; |
| | | while((queue->remove_timeout(item, &timeout)) ) { |
| | | // cout << "出队:" << item.pic << ", " << item.info << endl; |
| | | while((queue->pop_timeout(item, &timeout)) ) { |
| | | printf("consumer(%d) 出队: {%d, %d}\n", targ->key, item.pic, item.info); |
| | | // cout << item.pic << endl; |
| | | |
| | | i++; |
| | |
| | | if(pthread_join(tids[i], &res[i])!=0) { |
| | | perror("productor pthread_join"); |
| | | } else { |
| | | fprintf(stderr, "cosumer %d 读取了 %ld 条数据\n", i, (long)res[i]); |
| | | fprintf(stderr, "cosumer(%d) 读取了 %ld 条数据\n", i, (long)res[i]); |
| | | } |
| | | } |
| | | |
| | | |
| | | destroy(); |
| | | mm_destroy(); |
| | | cerr << "cosumer quit" << endl; |
| | | exit(EXIT_SUCCESS); |
| | | } |