克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ThreadPool 线程池

线程互斥

使用std::unique_lockstd::atomic实现不同线程互斥访问临界资源。

线程通信

使用std::condition_variable实现线程消费和任务提交的同步。

任务队列

任务用std::function<void()>类型进行封装。

任务返回值获取

返回值通过std::futurestd::packaged_task异步获取返回值。

使用方法

克隆本仓库

git clone https://gitee.com/llxGiteeSpace/thread-pool.git

新建build文件夹

mkdir build

构建静态/动态库文件,可在cmake中修改

cd build
cmake ..
make ThreadPool

使用示例:

#include <iostream>
#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<int> result1 = pool.submitTask(sum, 1, 2);
    std::future<int> result2 = pool.submitTask(sum, 3, 4);
    std::cout << result1.get() << std::endl;
    std::cout << result2.get() << std::endl;

    return 0;
}
MIT License Copyright (c) 2024 海绵宝宝卖海绵 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

学写一下线程池。用了c++11之后的许多新特性实现。 展开 收起
C++ 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化