-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfixedsizeq_tester.cc
141 lines (101 loc) · 2.99 KB
/
fixedsizeq_tester.cc
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Hossein Moein
// August 21, 2007
#include <cstdio>
#include <ctime>
#include <iostream>
#include <thread>
#include <Bobcat/FixedSizeQueue.h>
using namespace hmq;
//-----------------------------------------------------------------------------
void *pusher ();
void *popper ();
void *counter ();
FixedSizeQueue<int> Q (10);
static const int MAX_COUNT = 100000;
//-----------------------------------------------------------------------------
struct Base {
int i_;
Base (int i) : i_ (i) { }
};
struct left : public virtual Base {
left () : Base (1) { }
};
struct right : public virtual Base {
right () : Base (2) { }
};
struct bottom : public left, public right {
bottom () : Base (3) { }
};
int main (int argc, char *argv []) {
bottom b;
std::cout << b.i_ << std::endl;
std::thread ts [3];
ts[0] = std::thread (&pusher);
ts[1] = std::thread (&popper);
ts[2] = std::thread (&counter);
for (size_t i = 0; i < 3; ++i)
ts [i].join();
return EXIT_SUCCESS;
}
//-----------------------------------------------------------------------------
void *counter () {
char buffer [1024];
int count = 0;
while (count++ < 60) {
#ifndef WIN32
const struct ::timespec rqt = {1, 0};
nanosleep (&rqt, NULL);
#endif // WIN32
snprintf (buffer, sizeof(buffer) - 1,
"The number of current elements "
"in the queue is: %d\n", Q.size ());
std::cout << buffer;
}
return NULL;
}
//-----------------------------------------------------------------------------
void *pusher () {
char buffer [1024];
size_t index = 0;
int count = 0;
while (count++ < MAX_COUNT) {
// if (!(index % 500)) {
// const struct ::timespec rqt = {5, 0};
// nanosleep (&rqt, NULL);
// }
Q.push (index);
snprintf (buffer, sizeof(buffer) - 1, "Pushed element %lu\n", index);
std::cout << buffer;
index += 1;
}
return NULL;
}
//-----------------------------------------------------------------------------
void *popper () {
char buffer [1024];
size_t index = 0;
int count = 0;
while (count++ < MAX_COUNT) {
index += 1;
if (! (index % 100)) {
int i = 0;
Q.pop_front (i);
snprintf (buffer, sizeof(buffer) - 1,
"Poped element (pop_front()) %d\n", i);
std::cout << buffer;
}
else {
const int i = Q.front ();
snprintf (buffer, sizeof(buffer) - 1, "Poped element %d\n", i);
std::cout << buffer;
Q.pop();
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Local Variables:
// mode:C++
// tab-width:4
// c-basic-offset:4
// End: