# ThreadPool **Repository Path**: llxGiteeSpace/thread-pool ## Basic Information - **Project Name**: ThreadPool - **Description**: 学写一下线程池。用了c++11之后的许多新特性实现。 - **Primary Language**: C++ - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-06-26 - **Last Updated**: 2024-07-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: threadpool, Cpp学习, cmake, Thread ## README # ThreadPool 线程池 ## 线程互斥 使用``std::unique_lock``和``std::atomic``实现不同线程互斥访问临界资源。 ## 线程通信 使用``std::condition_variable``实现线程消费和任务提交的同步。 ## 任务队列 任务用``std::function``类型进行封装。 ## 任务返回值获取 返回值通过``std::future``和``std::packaged_task``异步获取返回值。 ## 使用方法 克隆本仓库 ``` git clone https://gitee.com/llxGiteeSpace/thread-pool.git ``` 新建build文件夹 ``` mkdir build ``` 构建静态/动态库文件,可在cmake中修改 ``` cd build cmake .. make ThreadPool ``` 使用示例: ``` #include #include "ThreadPool.h" int sum(int a, int b) { // std::cout << "sum" << a << b << std::endl; return a+b; } int main() { std::cout << "hello threadpool." << std::endl; ThreadPool pool; pool.start(4); // 确定线程数量 std::future result1 = pool.submitTask(sum, 1, 2); std::future result2 = pool.submitTask(sum, 3, 4); std::cout << result1.get() << std::endl; std::cout << result2.get() << std::endl; return 0; } ```