1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| #include "shm_queue_wrapper.h"
| #include "mm.h"
|
| typedef struct message_t
| {
| char method[20];
| int code;
|
| } message_t;
|
| void test1() {
| unsigned int i = 0;
| int key = 2;
|
| size_t qsize = 16;
| void * queue = shmqueue_init( key, qsize, sizeof(message_t));
| message_t item;
|
| for(i = 0; i < qsize; i++) {
| sprintf(item.method, "hello");
| item.code = i ;
| //入队
| if(shmqueue_push(queue, (void *)&item)) {
| printf("push:%d %s\n", item.code, item.method );
| }
| }
|
| struct timespec timeout = {1, 0};
|
| i = 0;
| // 出队
| while((shmqueue_pop_timeout(queue, (void *)&item, &timeout)) ) {
| printf("pop:%d %s\n", item.code, item.method );
| // cout << item.pic << endl;
| i++;
| }
|
| //销毁队列
| shmqueue_destroy(queue);
| }
|
|
| int main () {
| test1();
|
| //整个进程退出时需要执行这个方法,该方法首先会检查是否还有其他进程在使用该共享内存,如果还有其他进程在使用就只是detach,如果没有其他进程在使用则销毁整块内存。
| mm_destroy();
| return 0;
| }
|
|