Open lix19937 opened 2 months ago
#include <iostream>
#include <vector>
#include <chrono>
#include "ThreadPool.h"
std::vector<std::vector<int>> a;
int main()
{
ThreadPool pool(10);
// create a <future> vector to wait the output
std::vector<std::future<std::vector<int>>> results;
std::vector<int> batch = {3, 3, 3};
int m = 3;
for (int b = 0; b < batch.size(); b++)
{
if (m > 0)
{
batch.push_back(4);
}
for (int i = 0; i < batch[b]; i++)
{
results.emplace_back(
pool.enqueue([i]
{
std::vector<int> res = {i};
std::cout << "hello " << i << " ";
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "world " << i << " ";
return res; }));
}
m--;
}
// get output
for (auto &&result : results)
a.emplace_back(result.get());
std::cout << std::endl;
// after get all output, print it
for (int i = 0; i < a.size(); i++)
{
for (int j = 0; j < a[i].size(); j++)
std::cout << a[i][j] << " ";
}
system("pause");
return 0;
}