thread test
void reduce(int *p, int idx, int num_thread) {
int res = 0;
for (int i = 0; i < 1000000.0 / num_thread; i++) {
for (int j = 0 ; j < 10000; j++)
res *= i;
}
p[idx] = res;
}
int main(int argc, char** argv) {
for (int iter = 1; iter < 50; ++iter) {
START_TIME_CLOCKER;
const int num_thread = iter;
int *p = new int[num_thread];
memset(p, 0, sizeof(int)*(num_thread));
std::vector<std::thread> threads;
std::thread *thread_pool_ = new std::thread[num_thread];
for (int i = 0; i < num_thread; i++) {
threads.push_back(std::thread(reduce, p, i, num_thread));
}
std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
std::cout << "total time: " << DURATION_TIME_CLOCKER << std::endl;
for (int i = 0; i < num_thread; i++) {
std::cout << p[i] << " ";
}
std::cout << std::endl;
}
}