wangzhengquan
2020-07-08 3710ce88088c00599c5b108456f6dde9a4d981bc
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "test.h"
 
using namespace std;
 
void testStruct() {
    unsigned int i = 0;
    
    int key = 2;
 
    struct Item item;
 
    size_t qsize = 16;
      //LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (key, qsize);
    SHMQueue<struct Item> *queue = new SHMQueue<struct Item>(key, 16);
    // LockFreeQueue<struct Item> queue(16);
    for(i = 0; i < qsize; i++) {
 
        if(queue->push({i, i})) {
             cout << i << " push:" << i << endl;
        }
    }
 
    for(i = 0; i < qsize; i++) {
        
        //queue.dequeue(item);
        
        item = (*queue)[i];
        cout << "i=" << i << " item " << item.pic << "," << item.info << endl;
    }
 
 
     
    struct timespec timeout = {1, 0};
 
    i = 0;
    while((queue->pop_timeout(item, &timeout)) ) {
        cout << i << " pop:" << item.pic << ", " << item.info << endl;
       // cout <<  item.pic << endl;
        i++;
    }
 
    delete queue;
}
 
void testString() {
    unsigned int i = 0;
    std::ostringstream outstr;
    int key = 2;
 
    shmstring item;
 
    size_t qsize = 16;
      //LockFreeQueue<struct Item> *queue = QueueFactory::createQueue<struct Item> (key, qsize);
    SHMQueue<shmstring> *queue = new SHMQueue<shmstring>(key, 16);
    // LockFreeQueue<struct Item> queue(16);
    for(i = 0; i < qsize; i++) {
        outstr.seekp(0);
        outstr << "hello " << i ; 
        if(queue->push(outstr.str().c_str())) {
             cout << i << " push:" << outstr.str() << endl;
        }
    }
 
    // for(i = 0; i < qsize; i++) {
        
    //     //queue.dequeue(item);
        
    //     item = (*queue)[i];
    //     cout << "i=" << i << ":" << item << endl;
    // }
 
 
     
    struct timespec timeout = {1, 0};
 
    i = 0;
    while((queue->pop_timeout(item, &timeout)) ) {
        cout << i << " pop:" << item << endl;
       // cout <<  item.pic << endl;
        i++;
    }
 
    delete queue;
}
 
 
int main () {
    testString();
 
 
    mm_destroy();
    return 0;
}