#include "test.h"
|
|
|
using namespace std;
|
int key = 2;
|
size_t qsize = 16;
|
|
void productor() {
|
// LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (key, qsize);
|
SHMQueue<struct Item> *queue = new SHMQueue<struct Item>(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++;
|
|
}
|
delete queue;
|
}
|
|
|
void consumer() {
|
SHMQueue<struct Item> *queue = new SHMQueue<struct Item>(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;
|
}
|
|
}
|
delete queue;
|
}
|
|
int main(int argc, char *argv[])
|
{
|
|
|
|
|
productor();
|
consumer();
|
|
// 销毁共享内存
|
mm_destroy();
|
exit(EXIT_SUCCESS);
|
}
|