A lightweight thread pool written in C++11, designed for simplicity and cross-platform usage.
- Easy to use
- Single Header
- Cross Platform
#include <kani/thread_pool.hpp>
using namespace kani;
// Uses the maximum available worker threads.
ThreadPool();
// Specifies the number of worker threads to use.
ThreadPool(size_t lenThreads);
// Single-threaded thread pool; tasks run one at a time.
OrderedThreadPool();
ThreadPool tp;
if (!tp.start()) { // start workers
return;
}
tp.enqueue([]() { // enqueue a task
// your task
});
auto res = tp.enqueue([](int32_t a1, int32_t a2) { // enqueue a task and
return a1 + a2; // capture the result as std::future
}, 1, 2);
std::cout << "Task result: " << res.get(); // output: "Task result: 3"
tp.stop(); // stop workers
- Add the following to your
CMakeLists.txt
to fetch and include the library:include(FetchContent) FetchContent_Declare( ThreadPool GIT_REPOSITORY https://github.com/kaniteru/CPP-ThreadPool GIT_TAG main # You can replace 'main' with a specific tag or branch if needed. ) FetchContent_MakeAvailable(ThreadPool)
- Link the library to your target:
target_link_libraries(MyProject PRIVATE ThreadPool)
- Clone the repository or download the source files:
git clone https://github.com/kaniteru/CPP-ThreadPool
- Add the
include
directory to your project manually or using CMake:target_include_directories(MyExecutable PRIVATE path/to/clone/include)
- support c++ 98
This project is licensed under the MIT License. Portions of the code were derived or inspired by projects licensed under the zlib License. For more details, refer to the LICENSE file.