A simple to use, robust and flexible thread pool written in C++20.
Features:
- Written in modern C++20
- Header-only
- Granular locking to improve mixed read/write loads
- High-level status interface
- Generic enqueuing interface allow any callable, not just functions
Basic usage looks like this:
// Initialize threadpool with four workers (threads)
jbo::thread_pool tp(4);
// Enqueue long running tasks
std::vector<std::future<int>> results;
for (std::size_t i = 0; i < 10; i++) {
// Enqueue using lambda
auto result = tp.enqueue([i]() -> int {
std::this_thread::sleep_for(std::chrono::seconds(1));
return i * i;
});
results.emplace_back(std::move(result));
}
// Collect results
for (auto& result : results)
std::cout << "result: " << result.get() << std::endl;