C++17 implementation of a thread pool. Requires some C++17 features (see Required language features).
#include <iostream>
#include <vector>
#include "threadpool.h"
int multiply(int x, int y) {
return x * y;
}
int main() {
thread_pool pool(4);
std::vector<std::future<int>> futures;
for (const int &x : { 2, 4, 7, 13 }) {
futures.push_back(pool.execute(multiply, x, 2));
}
for (auto &fut : futures) {
std::cout << fut.get() << std::endl;
}
return 0;
}
std::is_invocable, std::is_invocable_v (C++17 feature)
std::invoke_result (C++17 feature)
move-only lambda capture (C++14 feature)
This may not compile without an explicit C++17 flag (depending on your compiler). If there are errors, double check the flags you pass to your compiler.
clang: -std=c++17
gcc: -std=c++17
msvc: /std:c++17