StdThreadPool/main.cpp

56 lines
1.7 KiB
C++

#include <iostream>
#include "include/ThreadPool.h"
void SumZeroToNumber(long &num) {
long val = 0;
for (long i = 0; i <= static_cast<long>(num); i++) {
val += i;
}
num = val;
}
int main() {
for (int tries = 0; tries < 10; tries++) {
std::cout << "Try number: " << tries + 1 << "... ";
std::vector<long> nums(100000);
std::vector<long> numsResults(nums.size());
std::vector<long> numsSequentialResults(nums.size());
try {
ThreadPool pool; // num of cores
pool.start();
for (unsigned i = 0; i < nums.size(); i++) {
nums[i] = rand() % 1000;
numsResults[i] = nums[i];
numsSequentialResults[i] = nums[i];
// this is just a copy to be passed and
// modified, while the original remains
// unchanged
pool.push(std::bind(SumZeroToNumber, std::ref(numsResults[i])));
}
// std::this_thread::sleep_for(std::chrono::milliseconds(1000));
pool.finish();
} catch (std::exception &ex) {
std::cout
<< "An exception was thrown while processing data. Exception says: "
<< ex.what() << std::endl;
std::exit(1);
}
// test results sequentially
for (unsigned i = 0; i < nums.size(); i++) {
SumZeroToNumber(numsSequentialResults[i]);
if (numsSequentialResults[i] != numsResults[i]) {
std::cout << "failed." << std::endl;
std::cout << "Comparing " << numsSequentialResults[i] << " with "
<< numsResults[i] << " failed." << std::endl;
throw std::runtime_error("Results didn't match!");
}
}
std::cout << "succeeded." << std::endl;
}
std::cout << "End of program reached." << std::endl;
return 0;
}