# threadpool **Repository Path**: naught/threadpool ## Basic Information - **Project Name**: threadpool - **Description**: 基于C++11线程池。 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 1 - **Created**: 2023-06-06 - **Last Updated**: 2026-08-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ThreadPool 一个轻量级的C++线程池实现。 ## 功能特性 - **固定数量线程池**:创建指定大小的线程池 - **全局线程池**:提供全局共享的线程池实例 - **线程状态监控**:支持查询线程池大小、空闲线程数和忙碌线程数 - **智能任务管理**:自动分配任务到工作线程 ## 快速开始 ### 环境要求 - C++11 或更高版本 - CMake 3.10+ ### 构建项目 ```bash mkdir build cd build cmake .. make ``` ### 使用示例 ```cpp #include "threadpool.h" #include int main(int argc, char *argv[]) { // 创建线程池,指定线程数量 auto pool = threadpool::create(4); // 提交任务 pool->submit([] { // 任务逻辑 }); // 获取线程池状态 std::cout << "线程池大小: " << pool->size() << std::endl; std::cout << "空闲线程: " << pool->idle() << std::endl; std::cout << "忙碌线程: " << pool->busy() << std::endl; // 使用全局线程池 auto globalPool = threadpool::global(); return 0; } ``` ## API 参考 ### 核心方法 - `static std::shared_ptr create(std::size_t size)` - 创建新的线程池 - `static std::shared_ptr global()` - 获取全局线程池实例 - `std::size_t size() const` - 获取线程池总大小 - `std::size_t idle() const` - 获取空闲线程数 - `std::size_t busy() const` - 获取忙碌线程数 ### 提交任务 通过 `submit()` 方法提交任务到线程池执行: ```cpp pool->submit([] { // 你的任务代码 }); ``` ## 许可证 本项目采用 MIT 许可证。