-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.cc
More file actions
46 lines (40 loc) · 988 Bytes
/
factory.cc
File metadata and controls
46 lines (40 loc) · 988 Bytes
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
#include <thread>
#include <iostream>
#include <chrono>
#include "chan/chan.h"
using namespace std;
void worker(chan<string> in, chan<string> out, string const &text)
try {
while(true) {
string product;
in >> product;
product += text;
out << product;
}
} catch (Channel<string>::ClosedException &e) {
out.close();
}
int main() {
// make channels and workers
chan<string> ch1, ch2, ch3;
chan<string> ch4(10); // some capacity
thread t1(worker, ch1, ch2, "two-story ");
thread t2(worker, ch2, ch3, "blue ");
thread t3(worker, ch3, ch4, "house.\n");
// make orders
for (size_t i = 0; i != 10; ++i) {
ch1 << "A ";
}
// get results
for (size_t i = 0; i != 10; ++i) {
cout << "Houses ready: " << ch4.size() << '\n';
string result;
ch4 >> result;
cout << result;
}
// clean up workers
ch1.close();
t1.join();
t2.join();
t3.join();
}