#include "test.h" using namespace std; int key = 2; size_t qsize = 16; // 销毁共享内存和信号 void sigint_handler(int sig) { destroy(key); exit(0); } void productor() { LockFreeQueue *queue = QueueFactory::createQueue (key, qsize); /* Transfer blocks of data from stdin to shared memory */ struct Item item; struct timespec timeout = {5, 0}; int i = 0; while(true) { item.pic = i; item.info = i; if(queue->push_nowait(item)) { cout << "入队:" << item.pic << ", " << item.info << endl; } else { cout << "队列已经满,push_nowait 返回" << endl; } if(queue->push_timeout(item, &timeout)) { cout << "入队:" << item.pic << ", " << item.info << endl; } else { cout << "队列已经满,等待5s, push_timeout 返回" << endl; break; } // if (i == (1 << 30)) // i = 0; //sleep(1); i++; } } void consumer() { LockFreeQueue *queue = QueueFactory::createQueue (key, qsize); /* Transfer blocks of data from shared memory to stdout */ while(1) { struct Item item; if (queue->pop_nowait(item)) { cout << "出队:" << item.pic << ", " << item.info << endl; } else { cout << "队列为空,pop_nowait返回" << endl; } struct timespec timeout = {5, 0}; if (queue->pop_timeout(item, &timeout)) { cout << "出队:" << item.pic << ", " << item.info << endl; } else { cout << "队列为空,等待5s,pop_timeout返回" << endl; break; } } } int main(int argc, char *argv[]) { signal(SIGINT, sigint_handler); productor(); consumer(); // 销毁共享内存和信号 destroy(key); exit(EXIT_SUCCESS); }