Header-only C++ thread pool with a safe task queue and future-based results.
Qthread combines a queue + thread pool in a simple, portable, single-header library. It makes parallel execution easy: submit lambdas, functors, or functions, and retrieve results via std::future.
- Header-only - just drop qthread.hpp into your project.
- Thread pool - manage a fixed number of worker threads.
- Safe Task queue - tasks are stored and processed in FIFO order.
- Future-based results - submit tasks and get back std::future<T>.
- No dependencies - pure C++ standard library.
Just copy the include/qthread.hpp into your codebase.
#include "qthread.hpp"
#include <iostream>
int main() {
auto poo = qthread::make_fifo_pool(4);
auto f1 = pool.submit([] { return 42; });
auto f2 = pool.submit([](int a, int b) { return a + b; }, 10, 32);
std::cout << "f1 = " << f1.get() << "\n";
std::cout << "f2 = " << f2.get() << "\n";
pool.wait_for_completion();
}
#include "qthread.hpp"
#include <iostream>
int main() {
auto pool = qthread::make_priority_pool(2);
pool.submit([] { std::cout << "Low priority\n"; }, qthread::Priority::Low);
pool.submit([] { std::cout << "High priority\n"; }, qthread::Priority::High);
pool.submit([] { std::cout << "Normal priority\n"; }, qthread::Priority::Normal);
pool.wait_for_completion();
}
cmake -S . -B build
cmake --build build
ctest --test-dir build --output-on-failure
Planned features for future versions:
- Task priorities.
- Task Cancellation
- Dynamic thread pool resizing
- Work-stealing scheduler
- Timeout and deadline support
- Thread affinity / CPU pinnig
- metrics and instrumentation
Contributions are Welcome!
- Fork the repo and create a new branch
- Add your changes with tests if possible
- Ensure formatting passes clang-format
- Submit a PR