-
Notifications
You must be signed in to change notification settings - Fork 1
/
cq_test.cpp
58 lines (45 loc) · 1.58 KB
/
cq_test.cpp
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
#include <iostream>
#include <sstream>
#include <functional>
#include <thread>
#include <cassert>
#include "include/ConcurrentQueue.h"
using MyQ = ConcurrentQueue<int>; // define the type of queue here
unsigned int unique = 0; // unique item id
void produce(MyQ& q, unsigned int producer, unsigned int iter) {
for (size_t i = 0; i < iter; ++i) {
q.push(++unique);
std::stringstream msg; // operator<< is non thread-safe
msg << "Producer " << producer << " ==> item " << unique << ", size = " << q.size() << std::endl;
std::cout << msg.str();
}
}
void consume(MyQ& q, unsigned int consumer, unsigned int iter) {
for (size_t i = 0; i< iter; ++i) {
auto item = q.pop();
std::stringstream msg;
msg << "Consumer " << consumer << " <== id " << item << ", size = " << q.size() << std::endl;
std::cout << msg.str();
}
}
int main()
{
MyQ q;
const int iter_prod = 10;
const int iter_cons = 10;
// producer threads
std::thread prod1(std::bind(&produce, std::ref(q), 1, iter_prod));
std::thread prod2(std::bind(&produce, std::ref(q), 2, iter_prod));
std::thread prod3(std::bind(&produce, std::ref(q), 3, iter_prod));
// consumer threads
std::thread consumer1(std::bind(&consume, std::ref(q), 1, iter_cons));
std::thread consumer2(std::bind(&consume, std::ref(q), 2, iter_cons));
std::thread consumer3(std::bind(&consume, std::ref(q), 3, iter_cons));
prod1.join();
prod2.join();
prod3.join();
consumer1.join();
consumer2.join();
consumer3.join();
assert(q.empty());
}