-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path103ProducerConsumerthreads.cpp
More file actions
102 lines (85 loc) · 2.04 KB
/
Copy path103ProducerConsumerthreads.cpp
File metadata and controls
102 lines (85 loc) · 2.04 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
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
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <unordered_set>
using namespace std;
vector<int> buffer;
mutex mtx;
condition_variable cv;
bool done = false;
// ---------------- PRODUCER ----------------
void producer(const vector<int>& input) {
for (int x : input) {
{
unique_lock<mutex> lock(mtx);
buffer.push_back(x);
cout << "Produced: " << x << endl;
}
cv.notify_one();
this_thread::sleep_for(chrono::milliseconds(100));
}
// signal finished
{
unique_lock<mutex> lock(mtx);
done = true;
}
cv.notify_one();
}
// ---------------- CONSUMER ----------------
void consumer() {
vector<int> consumed;
while (true) {
unique_lock<mutex> lock(mtx);
cv.wait(lock, [] {
return !buffer.empty() || done;
});
while (!buffer.empty()) {
int val = buffer.back();
buffer.pop_back();
consumed.push_back(val);
cout << "Consumed: " << val << endl;
}
if (done) break;
}
// ----- REMOVE DUPLICATES -----
unordered_set<int> seen;
vector<int> unique;
for (int x : consumed) {
if (seen.insert(x).second) {
/*
For set / unordered_set:
insert(value)
returns:
pair<iterator, bool>
• .first → iterator to element
• .second → true if insertion succeeded
• .second → false if element already existed
So:
seen.insert(x).second
means:
👉 “Was x NOT already present?”
If true:
first time seeing x
insert into unique list
If false:
duplicate → skip
*/
unique.push_back(x);
}
}
cout << "\nAfter removing duplicates:\n";
for (int x : unique)
cout << x << " ";
cout << endl;
}
// ---------------- MAIN ----------------
int main() {
vector<int> input = {1, 2, 3, 4, 2, 5, 3, 6};
thread t1(producer, cref(input));
thread t2(consumer);
t1.join();
t2.join();
return 0;
}