-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
47 lines (39 loc) · 879 Bytes
/
main.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
#include "thread_safe_queue.h"
#include <thread>
#include <iostream>
#include <functional>
namespace
{
struct QueueEntry
{
static size_t ctr;
size_t msg;
};
size_t QueueEntry::ctr = 0;
QueueEntry CreateANewEntry() { return QueueEntry{ ++QueueEntry::ctr }; }
}
int main()
{
ThreadSafeQueue<QueueEntry> TSQ;
auto producer = [](ThreadSafeQueue<QueueEntry>& TSQ) {
for (size_t i = 0; i != 1000; ++i)
{
TSQ.push(CreateANewEntry());
}
};
auto consumer = [](ThreadSafeQueue<QueueEntry>& TSQ) {
for (size_t i = 0; i != 1000; ++i)
{
std::cout << TSQ.pop().msg << "\n";
}
};
std::thread c1(consumer, std::ref(TSQ));
std::thread c2(consumer, std::ref(TSQ));
std::thread p1(producer, std::ref(TSQ));
std::thread p2(producer, std::ref(TSQ));
p1.join();
p2.join();
c1.join();
c2.join();
return 0;
}