Newer
Older
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 <chrono>
#include <deque>
#include <mutex>
#include <thread>
using namespace std::literals::chrono_literals;
#include "async_containers.hpp"
#include "items.hpp"
template <size_t sleep_duration_ms> struct Producer_AsyncQueue {
Item consume() { return channel.consume(); }
void produce(size_t n_times) {
for (size_t i = 0; i < n_times; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds{sleep_duration_ms});
channel.produce(Item{(int)i});
}
channel.finish();
}
void reset() { channel.reset(); }
private:
AsyncQueue<Item> channel;
};
template <size_t sleep_duration_ms> struct Producer_Async_Iterator {
// TODO: implement forward input iterator interface
// TODO: decide if that's here, or a separate struct
// operator*() {}
// operator++() {}
Item consume() { return channel.consume(); }
void produce(size_t n_times) {
for (size_t i = 0; i < n_times; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds{sleep_duration_ms});
channel.produce(Item{(int)i});
}
channel.finish();
}
void reset() { channel.reset(); }
private:
AsyncQueue<Item> channel;
};