I am not a good C++ programmer, but currently using some features of C++ to clean up dirty parts of my C code. The g++ compiler complains about threads[i] = thread(split, i, sums[i], from, to, f, nThreads);
. Please help me find the problem.
// mjArray
is just a thin class to use instead of std::vector which is too heavy in my case.
#include <cstdio>
#include <cmath>
#include <ctime>
#include <thread>
using namespace std;
template<typename T>
class mjArray {
private:
T* _array;
int _length;
public:
mjArray(int length) {
_array = new T[length];
_length = length;
}
mjArray(int length, T val) {
_array = new T[length];
_length = length;
for (int i = 0; i < length; ++i) {
_array[i] = val;
}
}
~mjArray() {
delete[] _array;
}
T& operator[](int i) {
return _array[i];
}
int length() {
return _length;
}
};
void split(int n, double& sum, int from, int to, double (*f)(double), int nThreads) {
for (int i = from + n; i <= to; i += nThreads) {
sum += f(i);
}
}
double sigma(int from, int to, double (*f)(double), int nThreads) {
double sum = 0.0;
mjArray<double> sums(nThreads, 0.0);
mjArray<thread> threads(nThreads);
for (int i = 0; i < nThreads; ++i) {
threads[i] = thread(split, i, sums[i], from, to, f, nThreads);
}
for (int i = 0; i < nThreads; ++i) {
threads[i].join();
sum += sums[i];
}
return sum;
}
double f(double x) {
return (4 / (8 * x + 1) - 2 / (8 * x + 4) - 1 / (8 * x + 5) - 1 / (8 * x + 6)) / pow(16, x);
}
int main(void) {
for (int i = 1; i <= 4; ++i) {
time_t start = clock();
double pi = sigma(0, 1000000, f, i);
time_t end = clock();
printf("pi = %.10f; nThreads = %d; elapsed = %.3fs\n", pi, i, (double)(end - start) / CLOCKS_PER_SEC);
}
return 0;
}