#include "test.h"
|
|
|
using namespace std;
|
|
SQueue<struct Item> *queue;
|
// 销毁共享内存和信号
|
void sigint_handler(int sig) {
|
queue->~SQueue();
|
mm_deinit();
|
exit(0);
|
|
}
|
|
int main(int argc, char *argv[])
|
{
|
|
|
void *shmp;
|
|
signal(SIGINT, sigint_handler);
|
|
int first = mm_init(sizeof(SQueue<struct Item>), &shmp);
|
|
if (first == 1)
|
queue = new(shmp) SQueue<struct Item>(20);
|
else
|
queue = (SQueue<struct Item> *) shmp;
|
|
/* 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->enqueue_nowait(item)) {
|
cout << "入队:" << item.pic << ", " << item.info << endl;
|
} else {
|
cout << "队列已经满,enqueue_nowait 返回" << endl;
|
}
|
|
|
if(queue->enqueue_timeout(item, &timeout)) {
|
cout << "入队:" << item.pic << ", " << item.info << endl;
|
} else {
|
cout << "队列已经满,等待5 s ,enqueue_timeout 返回" << endl;
|
}
|
|
if (i == (1 << 30))
|
i = 0;
|
//sleep(1);
|
i++;
|
|
}
|
|
// 销毁共享内存和信号
|
queue->~SQueue();
|
mm_deinit();
|
exit(EXIT_SUCCESS);
|
}
|