-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_queue_example.cpp
More file actions
61 lines (52 loc) · 1.74 KB
/
circular_queue_example.cpp
File metadata and controls
61 lines (52 loc) · 1.74 KB
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
#include "circular_queue.h"
#include <boost/thread/thread.hpp>
#include <iostream>
//extreme test: pushing and popping 10million items,
//with 20 pushing and 20 popping thread(total of 40 threads) on a 16 item circular queue. :)
const int pushValue = 1;
const int taskCount = 10000000;
const int pushingThreadCount = 20;
const int poppingThreadCount = 20;
circular_queue<int, 16u> tasks;
int result[poppingThreadCount];
void pushData(int id){
boost::this_thread::disable_interruption di;
int count = taskCount/pushingThreadCount;
// when taskCount is not dividable with pushingThreadCount,
// then some threads needs to run once more.
if(taskCount%pushingThreadCount>id)
count++;
for(int i=0;i<count;i++)
tasks.push(pushValue);
}
void popData(int id){
boost::this_thread::disable_interruption di;
try {
while(1)
result[id] += tasks.pop();
} catch(exNoMorePush ex){}
}
int main(){
boost::thread* pushingThreads[pushingThreadCount];
boost::thread* poppingThreads[poppingThreadCount];
for(int i=0;i<pushingThreadCount;i++){
pushingThreads[i] = new boost::thread(boost::bind(pushData,i));
}
for(int i=0;i<poppingThreadCount;i++){
poppingThreads[i] = new boost::thread(boost::bind(popData,i));
}
for(int i=0;i<pushingThreadCount;i++){
pushingThreads[i]->join();
std::cout << "pushing thread " << i << " completed!" << std::endl;
}
tasks.signalNoMorePush();
int final_result=0;
for(int i=0;i<poppingThreadCount;i++){
poppingThreads[i]->join();
final_result += result[i];
std::cout << "popping thread " << i << " completed: " << result[i] << " elements popped" <<std::endl;
}
std::cout << "Value should be: " << pushValue * taskCount << std::endl;
std::cout << "Calculated value: " << final_result << std::endl;
std::cin.get();
}