基于 C++ 11 实现线程池
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
louyu f4cddc2d13 更新部分注释 10 months ago
README.md first commit 10 months ago
ThreadPool.h 更新部分注释 10 months ago
main.cpp first commit 10 months ago

README.md

基于C++11 实现线程池

项目描述:

  1. 基于可变参模板编程和引用折叠原理,实现线程池submitTask接口,支持任意任务函数和任意参数的传递;
  2. 使用future类型定制submitTask提交任务的返回值;
  3. 使用map和queue容器管理线程对象和任务;
  4. 基于条件变量condition_variable和互斥锁mutex实现任务提交线程和任务执行线程间的通信机制;
  5. 支持fixed和cached模式的线程池定制。

使用示例:

#include <iostream>
#include <future>
#include "ThreadPool.h" // 引入头文件

using namespace std;

int sum1(int a, int b) {
    return a + b;
}

int main() {
    ThreadPool pool; // 定义线程池对象
    pool.start(); // 启动线程池
    future<int> res = pool.submitTask(sum1, 10, 20); // 提交异步任务

    cout << res.get() << endl; // 打印结果

    return 0;
}

更多进阶用法详见头文件中的注释说明。