39 lines
1.1 KiB
C++
39 lines
1.1 KiB
C++
#include "../include/ThreadPool_pthread.h"
|
|
#include <iostream>
|
|
#include <cstdlib>
|
|
|
|
void* SumZeroToNumber(void* num)
|
|
{
|
|
long val = 0;
|
|
for(long i = 0; i <= *static_cast<long*>(num); i++) {
|
|
val += i;
|
|
}
|
|
*static_cast<long*>(num) = val;
|
|
return NULL;
|
|
}
|
|
int main()
|
|
{
|
|
std::vector<long> nums(10000);
|
|
std::vector<long> numsResults(10000);
|
|
try
|
|
{
|
|
ThreadPool_pthread pool(8); //num of cores
|
|
for(unsigned i = 0; i < nums.size(); i++) {
|
|
nums[i] = rand() % 1000;
|
|
numsResults[i] = nums[i]; //this is just a copy to be passed and modified, while the original remains unchanged
|
|
pool.push_task(SumZeroToNumber,static_cast<void*>(&numsResults[i]));
|
|
}
|
|
pool.finish();
|
|
}
|
|
catch(std::exception &ex)
|
|
{
|
|
std::cout<<"An exception was thrown while processing data. Exception says: " << ex.what() << std::endl;
|
|
std::exit(1);
|
|
}
|
|
for (unsigned int i = 0; i < nums.size(); ++i) {
|
|
std::cout<<"Sum from 0 to " << nums[i] << " is: " << numsResults[i] <<std::endl;
|
|
}
|
|
std::cout<<"End of program reached."<<std::endl;
|
|
return 0;
|
|
}
|