1 Star 0 Fork 3

bugerking/ThreadPool

forked from Rocky/ThreadPool 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
example2.cpp 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
rocky 提交于 2023-06-18 19:47 . new pool
#include <cstdio>
#include "threadpool2/thread_pool.hpp"
float Sum(float end)
{
float s = 0;
float i = 0.0f;
while (i < end) {
s += i;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
i += 0.5;
}
return s;
}
struct SumFunctor {
float end;
SumFunctor(float e) : end(e) {}
float operator()()
{
float s = 0;
float i = 0.0f;
while (i < end) {
s += i;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
i += 0.5;
}
return s;
}
};
int main(int argc, char **argv) {
ThreadPool pool;
std::vector<std::future<float>> futures;
for (int i = 0; i < 20; i++) {
futures.emplace_back(pool.commit([](int k) -> float {
float s = 0;
for (int j = 0; j < k; j++) {
s += j;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
return s;
}, i));
}
for (auto &f : futures) {
auto ret = f.get();
printf("[lambda] result: %f\n", ret);
}
futures.clear();
float i = 100;
while(i < 110) {
futures.emplace_back(pool.commit(Sum, i));
std::this_thread::sleep_for(std::chrono::seconds(1));
i += 1.0f;
}
for (auto &f : futures) {
auto ret = f.get();
printf("[function] result: %f\n", ret);
}
futures.clear();
while(i < 120) {
futures.emplace_back(pool.commit(SumFunctor(i)));
std::this_thread::sleep_for(std::chrono::seconds(1));
i += 1.0f;
}
for (auto &f : futures) {
auto ret = f.get();
printf("[functor] result: %f\n", ret);
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bugerking/thread-pool.git
git@gitee.com:bugerking/thread-pool.git
bugerking
thread-pool
ThreadPool
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385