put your tasks into threads
#include"ThreadPool.h"
#include<iostream>voidprintNumber(int number) { std::cout << "Number: " << number << std::endl; }
intmain() {
ThreadPool pool(4);
std::vector<std::future<void>> results;
// starting the threadsfor (int i = 0; i < 8; ++i) {
results.emplace_back(pool.enqueue(printNumber, i));
}
// getting the resultsfor (auto &&result : results) {
result.get();
}
return0;
}multiply example
#include"ThreadPool.h"
#include<iostream>intmultiply(constint a, constint b) {
return a * b;
}
intmain() {
ThreadPool pool(12);
std::future<int> result = pool.enqueue(multiply, 3, 5);
std::cout<< result.get();
return0;
}