-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path05_producers_with_consumers.cpp
More file actions
48 lines (37 loc) · 1.75 KB
/
05_producers_with_consumers.cpp
File metadata and controls
48 lines (37 loc) · 1.75 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
#include <iostream>
#include "tdp/pipeline.hpp"
//---------------------------------------------------------------------------------------------------------------------
// This example shows how producers work in combination to consumers
//---------------------------------------------------------------------------------------------------------------------
// We will use a functor class for the consumer
// It can have anything inside it, it just needs to be move-constructible.
struct consume {
// We will print a count of how many times it consumed something
int count = 0;
~consume() {
if (count) // Stuff is move-constructed, so we may have a few more destruction calls...
std::cout << "The consumer was called " << count << " times.\n";
}
template <typename T>
void operator()(T) {
count++;
}
};
int main() {
using namespace std::chrono_literals;
// A Producer that returns an integer and takes 10ms to execute
constexpr auto get_int = []() -> int {
std::this_thread::sleep_for(10ms);
return -1;
};
// Our processing function
constexpr auto square = [](auto x) { return x * x; };
// When the processing is faster than the production, it's usually a good idea to use a blocking policy.
// And, as a queue will not be bigger than one, we can use a buffer instead.
// Experiment changing it to a queue and notice it won't make a difference in this case.
auto pipe = tdp::producer{get_int} >> square >> tdp::consumer{consume()} / tdp::policy::triple_buffer;
// We can do anything in this thread, including sleep. The pipeline keeps running on its threads.
std::this_thread::sleep_for(200ms);
// We exit the program.
// The consumer will be destroyed and, on destruction, it will print how many times it was called.
}