{"id":28717405,"url":"https://github.com/hsll175848494/threadpool","last_synced_at":"2025-06-15T03:15:37.767Z","repository":{"id":297550455,"uuid":"997131186","full_name":"HSLL175848494/ThreadPool","owner":"HSLL175848494","description":"高性能C++11线程池，具备线程独享任务队列、CPU核心绑定、栈上分配任务、任务窃取及优先任务支持","archived":false,"fork":false,"pushed_at":"2025-06-13T06:26:57.000Z","size":139,"stargazers_count":37,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-13T07:30:23.996Z","etag":null,"topics":["blockqueue","cpp11","threadpool"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HSLL175848494.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-06T02:29:55.000Z","updated_at":"2025-06-13T07:13:33.000Z","dependencies_parsed_at":"2025-06-06T20:01:16.417Z","dependency_job_id":null,"html_url":"https://github.com/HSLL175848494/ThreadPool","commit_stats":null,"previous_names":["hsll175848494/threadpool"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/HSLL175848494/ThreadPool","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HSLL175848494%2FThreadPool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HSLL175848494%2FThreadPool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HSLL175848494%2FThreadPool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HSLL175848494%2FThreadPool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HSLL175848494","download_url":"https://codeload.github.com/HSLL175848494/ThreadPool/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HSLL175848494%2FThreadPool/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259914924,"owners_count":22931334,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blockqueue","cpp11","threadpool"],"created_at":"2025-06-15T03:15:37.006Z","updated_at":"2025-06-15T03:15:37.735Z","avatar_url":"https://github.com/HSLL175848494.png","language":"C++","readme":"# HSLL::ThreadPool\n\n## 概述\nHSLL::ThreadPool 一个轻量级C++11线程池实现，具有以下核心特性：\n\n1. **多队列架构** - 每个工作线程拥有独立的任务队列(固定容量)，减少锁争用 \n2. **核心绑定** - 支持将工作线程绑定到指定CPU核心(Linux/Windows), 避免缓存失效\n3. **负载均衡** - 采用round-robin+二级队列选取机制+任务窃取机制实现负载均衡\n4. **定长任务容器** - 基于栈的预分配任务容器，将所有参数储存在栈上，避免动态申请空间\n5. **多提交接口** - 提供阻塞/非阻塞、单任务/批量任务等多种接口\n6. **双端插入支持** - 支持从队列头/尾插入以适应不同任务优先级\n7. **优雅关闭** - 支持立即关闭和等待任务完成的优雅关闭模式\n\n## 引入\n```cpp\n//依赖于TPBlockQueue.hpp和TPTask.hpp，请确保处于ThreadPool.hpp同级目录\n#include \"ThreadPool.hpp\"\n```\n\n## 核心组件\n\n### ThreadPool 类模板\n\n#### 模板参数\n```cpp\ntemplate \u003cclass T = TaskStack\u003c\u003e\u003e\nclass ThreadPool\n```\n- `T`: 任务类型，需实现 `execute()` 方法，默认使用基于栈的预分配任务容器\n\n#### 初始化方法\n```cpp\nbool init(unsigned queueLength, unsigned threadNum, unsigned batchSize = 1)\n```\n- **参数**：\n  - `queueLength`: 每个工作队列的容量\n  - `threadNum`: 工作线程数量\n  - `batchSize`: 单次处理任务数（单次从任务队列中取出多少任务）\n- **返回值**：初始化成功返回true\n- **功能**：分配资源并启动工作线程\n\n#### 任务提交接口\n\n```cpp\n\nenum INSERT_POS\n{\n    TAIL, //插入到队列尾部 \n    HEAD  //插入到队列头部\n};\n```\n\n1. **单任务提交(就地构造)**\n```cpp\ntemplate \u003cINSERT_POS POS = TAIL, typename... Args\u003e\nbool emplace(Args \u0026\u0026...args)\n\ntemplate \u003cINSERT_POS POS = TAIL, typename... Args\u003e\nbool wait_emplace(Args \u0026\u0026...args)\n\ntemplate \u003cINSERT_POS POS = TAIL, class Rep, class Period, typename... Args\u003e\nbool wait_emplace(const std::chrono::duration\u003cRep, Period\u003e \u0026timeout, Args \u0026\u0026...args)\n```\n\n2. **单任务提交**\n```cpp\ntemplate \u003cINSERT_POS POS = TAIL, class U\u003e\nbool enqueue(U \u0026\u0026task)\n\ntemplate \u003cINSERT_POS POS = TAIL, class U\u003e\nbool wait_enqueue(U \u0026\u0026task)\n\ntemplate \u003cINSERT_POS POS = TAIL, class U, class Rep, class Period\u003e\nbool wait_enqueue(U \u0026\u0026task, const std::chrono::duration\u003cRep, Period\u003e \u0026timeout)\n```\n\n3. **批量提交**\n```cpp\ntemplate \u003cBULK_CMETHOD METHOD = COPY, INSERT_POS POS = TAIL\u003e\nunsigned int enqueue_bulk(T *tasks, unsigned int count)\n\ntemplate \u003cBULK_CMETHOD METHOD = COPY, INSERT_POS POS = TAIL\u003e\nunsigned int wait_enqueue_bulk(T *tasks, unsigned int count)\n\ntemplate \u003cBULK_CMETHOD METHOD = COPY, INSERT_POS POS = TAIL, class Rep, class Period\u003e\nunsigned int wait_enqueue_bulk(T *tasks, unsigned int count, const std::chrono::duration\u003cRep, Period\u003e \u0026timeout)\n```\n```cpp\nenum BULK_CMETHOD\n{\n  COPY, // 使用拷贝语义，将任务拷贝到队列。原任务数组依旧有效\n  MOVE  // 使用移动语义，将任务移动到队列\n};\n```\n#### 关闭方法\n```cpp\nvoid exit(bool shutdownPolicy = true)\n```\n- `shutdownPolicy`: \n  - true: 优雅关闭（执行完队列剩余任务）\n  - false: 立即关闭\n\n#### 工作机制\n- **任务分发**：采用轮询+跳半队列负载均衡策略\n- **工作线程**：每个线程优先处理自己的队列，空闲时支持任务窃取\n- **核心绑定**：自动将工作线程绑定到不同CPU核心（需硬件支持）\n\n\n### 基本使用\n```cpp\nHSLL::ThreadPool\u003c\u003e pool;\npool.init(1000, 4); // 4线程，每队列容量1000\n\n// 提交lambda任务\npool.emplace([]{\n    std::cout \u003c\u003c \"Task executed!\\n\";\n});\n\n// 提交带参数的函数\nvoid taskFunc(int a, double b) { /*...*/ }\n\n//添加任务示例\npool.enqueue(taskFunc, 42, 3.14);\n\n//异步示例\nstd::promise\u003cint\u003e resultPromise;\nauto resultFuture = resultPromise.get_future();\npool.emplace([\u0026resultPromise] {\n      int sum = 0;\n      for (int i = 1; i \u003c= 100; i++) {\n        sum += i;\n   }\n  resultPromise.set_value(sum); \n});\nint total = resultFuture.get();\n\n//线程池析构时自动调用exit(false), 但仍然建议手动调用以控制退出行为\npool.exit(true); // 优雅关闭\n```\n\n### 任务生命周期\n```mermaid\ngraph TD\n    A[任务提交] --\u003e B{提交方式}\n    B --\u003e|emplace/wait_emplace| C[在队列存储中\u003cbr/\u003e直接构造任务]\n    B --\u003e|enqueue/wait_enqueue| D[用户构造任务对象\u003cbr/\u003e拷贝/移动到队列存储]\n    \n    C --\u003e E[工作线程从队列取出任务]\n    D --\u003e E\n    \n    E --\u003e F[在预分配执行内存上\u003cbr/\u003e就地构造（移动构造）]\n    F --\u003e G[执行execute方法]\n    G --\u003e H[显式调用析构函数]\n    H --\u003e I[清理执行内存]\n```\n\n### 性能优化建议\n1. **批量提交**：优先使用`enqueue_bulk`处理任务组\n2. **任务大小**：根据任务复杂度选择合适存储模板参数\n3. **线程数量**：通常设置为CPU核心数\n4. **队列容量**：根据任务吞吐量需求调整\n\n## 注意事项\n1. **类型匹配**：提交任务类型必须严格匹配队列任务类型\n2. **对齐要求**：任务最大对齐值必须小于等于队列任务类型的对齐值\n3. **异常安全**：\n   - 任务允许在默认构造时抛出异常，但拷贝/移动构造不允许抛出异常\n   - execute()方法不允许抛出异常，需要在任务内部捕获并处理所有可能的异常\n4. **参数传递**：\n   - 大型对象应使用指针或移动语义传递\n   - 避免在任务中捕获可能失效的引用\n5. **结果获取**：\n   - 使用std::promise/std::future获取异步结果\n   - promise必须在任务执行期间保持有效\n\n\n## 接口对比表\n\n| 方法类型      | 非阻塞      | 阻塞等待    | 超时等待      |\n|-------------|------------|------------|--------------|\n| 单任务提交    | emplace    | wait_emplace| wait_emplace |\n| 预构建任务   | enqueue     | wait_enqueue| wait_enqueue  |\n| 批量任务     | enqueue_bulk| wait_enqueue_bulk | wait_enqueue_bulk |\n\n## 平台支持\n- Linux (pthread affinity)\n- Windows (SetThreadAffinityMask)\n- C++11 或更新标准\n\n## 其它\n- **组件文档**: `document`\n- **性能测试**: `performance test`\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhsll175848494%2Fthreadpool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhsll175848494%2Fthreadpool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhsll175848494%2Fthreadpool/lists"}