-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy paththreadpool.cpp
More file actions
41 lines (36 loc) · 1.1 KB
/
threadpool.cpp
File metadata and controls
41 lines (36 loc) · 1.1 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
#include "../src/netlib.hpp"
#include <cstdint>
#include <iostream>
#include <optional>
#include <future>
#include <mutex>
#include <sstream>
#include <thread>
#include <vector>
int main(int argc, char** argv) {
netlib::thread_pool thread_pool;
auto do_something = [&](uint32_t some_var) -> std::string {
std::stringstream ss;
ss << std::this_thread::get_id() << "," << some_var;
//simulate some work
std::this_thread::sleep_for(std::chrono::milliseconds (100));
return ss.str();
};
std::vector<std::future<std::string>> futures;
for (int i = 0; i < 1000; ++i) {
futures.emplace_back(thread_pool.add_task(do_something, i));
}
uint32_t index = 0;
while (index != futures.size()) {
for (auto& fut : futures) {
if (fut.valid()) {
std::future_status fs = fut.wait_for(std::chrono::microseconds (50000));
if (fs == std::future_status::ready) {
std::cout << index << " finished, latest: " << fut.get() << std::endl;
index++;
}
}
}
std::cout << "Looped, tasks remaining: " << thread_pool.get_task_count() << std::endl;
}
}