double seriesTaylor int start int endN std cout start endN std endl in

 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
50
double seriesTaylor(int start, int endN)
{
std::cout << start << " " << endN << std::endl;
int i = start;
int n = endN;
double a = 1.;
double d = 0., f = 0.;
double pi = 0.;
for (i; i <= n; i++)
{
d = (double)1/(i+2);
f = (double)pow(-1, i) * d;
a += f;
}
pi = 4*a;
std::cout << pi << std::endl;
return pi;
}
struct Data{
int start;
int endN;
};
static void *func(void *lpParam)
{
struct Data dat = *((struct Data *)lpParam);
std::cout << dat.start << " " << dat.endN << std::endl;
double* result = (double*) malloc(sizeof(double));
*result = seriesTaylor(dat.start, dat.endN);
std::cout << *result << std::endl;
return result;
}
int main (int argc, char *argv[])
{
const int N = 4;
int iterations = 1000;
pthread_t *th = new pthread_t[N];
for (int i = 0; i < N; i++){
Data dat;
if (i == 0){
dat.start = 1;
dat.endN = iterations / N;
}
else{
dat.start = iterations / N * i;
dat.endN = iterations / N * i + iterations / N;
}
std::cout << dat.start << " " << dat.endN << std::endl;
pthread_create(&th[i], NULL, func, &dat);
}
}