-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (57 loc) · 1.61 KB
/
main.cpp
File metadata and controls
72 lines (57 loc) · 1.61 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
#include <atomic>
#include <iostream>
#include <thread>
#include <chrono>
#include <cstdlib>
#include <deque>
#include <async.h>
using namespace std;
size_t work(size_t i) {
//std::cout << "==> Work: " << i << endl;
auto r = std::rand() % 10;
std::this_thread::sleep_for(std::chrono::milliseconds(100 + r));
return i;
}
void dump_results(std::deque<size_t>& results) {
while (!results.empty()) {
auto result = results.front();
std::cout << " Result: " << result << std::endl;
results.pop_front();
}
}
int main(int argc, char** argv) {
const size_t pool_size = 2;
std::atomic<int> active_thread_count(0);
async::pool pool(pool_size);
async::queue lock_queue;
std::deque<size_t> results;
// Image capture loop
for (size_t i = 0; i < 100; ++i) {
// Run async process that spans multiple iterations
// Control number of outstanding tasks via active_thread_count
if (active_thread_count < pool_size) {
std::cout << "Process: " << i << std::endl;
pool.async([&, i]{
active_thread_count++;
auto result = work(i);
// lock_queue makes this operation thread-safe
lock_queue.sync([&]() {
results.push_back(result);
});
active_thread_count--;
});
}
else {
std::cout << " Skip: " << i << std::endl;
}
// Integrate most recent results. lock_queue makes this operation thread-safe.
lock_queue.sync([&]() {
dump_results(results);
});
// Main loop rate
std::this_thread::sleep_for(std::chrono::milliseconds(30));
}
pool.wait();
dump_results(results);
return 0;
}