-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread0x.cpp
49 lines (38 loc) · 1.13 KB
/
thread0x.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <vector>
#include <thread>
#include <iostream>
/**
* This example uses c++11.
* For now compile using -std=c++0x -pthread
**/
using namespace std;
static const int num_threads = 10;//5;//20;
/**
* Calculates the power given by pow of all values in the given vector from index f to t.
**/
void vectorPow(const int &thread_id, vector<double> &vector, const int &pow, const int &f, const int &t) {
//cout << "Thread " << thread_id << " working from " << f << " to " << t << endl;
for (int i = f; i < t; ++i) {
const double value = vector[i];
for (int j = 0; j < pow-1; ++j) {
vector[i] *= value;
}
}
}
int main() {
const int N = 1e6;
vector<double> vector;
int pow = 2000;
thread t[num_threads];
const int idx_per_thread = N / num_threads; // TODO: Handle rest indices if num_threads is not i multiple of N
for (int i = 0; i < N; ++i) {
vector.push_back(i);
}
for (int i = 0; i < num_threads; ++i) {
t[i] = thread(vectorPow, i, vector, pow, i*idx_per_thread, (i+1)*idx_per_thread);
}
for (int i = 0; i < num_threads; ++i) {
t[i].join();
}
return 0;
}