#include "test.h" using namespace std; void sigint_handler(int sig) { destroy(); exit(0); } void* run (void *arg) { struct Targ * targ = (struct Targ * )arg; // SArrayLockFreeQueue *queue = QFactory::createArrayLockFreeQueue (targ->key, 10); SLinkedLockFreeQueue *queue = QFactory::createLinkedLockFreeQueue (targ->key, 10); struct Item item; struct timespec timeout = {5, 0}; int i = 0; while((queue->remove_timeout(item, &timeout)) ) { // cout << "出队:" << item.pic << ", " << item.info << endl; // cout << item.pic << endl; i++; } return (void *)i; } int main(int argc, char *argv[]) { signal(SIGINT, sigint_handler); /* Create set containing two semaphores; initialize so that writer has first access to shared memory. */ int i; pthread_t tids[NTHREADS]; void *res[NTHREADS]; struct Targ targs[NTHREADS]; 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++) { if(pthread_join(tids[i], &res[i])!=0) { perror("productor pthread_join"); } else { fprintf(stderr, "cosumer %d 读取了 %ld 条数据\n", i, (long)res[i]); } } destroy(); cerr << "cosumer quit" << endl; exit(EXIT_SUCCESS); }