Pthreads help needed

Hello i'm a bit new to c++ and i made a program for 4 threads to count pi but i don't know how to attach the info to the threads and get it back to main.I'm new to this language please understand the very big mistake i did.If it was correct i wouldn't ask for help

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
51
52
53
54
55
56
57
#include <iostream>
#include <math.h>
#include <pthread.h>javascript:PostPreview()
#include <cstdlib>
//Gottfried Wilhelm Leibniz Formula

using namespace std;

int main ()
{
  long double af1=0;
long long int d1=3, N1=0, M1=1000000000;
while (N1 < M1)
  {
  af1 = (4.0 / d1) + af1;
  d1 = d1 + 8;
  N1 = N1 + 1;
  }


  long double af2=0;
long long int d2=7, N2=0, M2=1000000000;
while (N2 < M2)
  {
  af2 = (4.0 / d2) + af2;
  d2 = d2 + 8;
  N2 = N2 + 1;
  }


  long double pr1=0;
long long int d3=1, N3=0, M3=1000000001;
while (N3 < M3)
  {
  pr1 = (4.0 / d3) + pr1;
  d3 = d3 + 8;
  N3 = N3 + 1;
  }


  long double pr2=0;
long long int d4=5, N4=0, M4=1000000000;
while (N4 < M4)
  {
  pr2 = (4.0 / d4) + pr2;
  d4 = d4 + 8;
  N4 = N4 + 1;
  }


cout.precision(64);
cout << pr1 + pr2 << endl;
cout << (af1 + af2) * -1 << endl;
cout << pr1 + pr2 + ((af1 + af2) * -1) << endl;

return 0;
}


to

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

#include <cstdlib>
//Gottfried Wilhelm Leibniz
//4 threads by Modiotis Athanasios

using namespace std;

void *print_message(void*)
{
     long double af1=0;
long long int d1=3, N1=0, M1=1000000000;
while (N1 < M1)
  {
  af1 = (4.0 / d1) + af1;
  d1 = d1 + 8;
  N1 = N1 + 1;
  }
  cout.precision(64);
  cout << af1 << endl;
  pthread_exit(NULL);
}


int main ()
{
    long double af1;
    pthread_t thread0, thread1, thread2, thread3;
    int C0, C1, C2, C3, h;
    C0 = pthread_create( &thread0, NULL, &print_message, NULL);
    cout << af1 << endl;
if (C0){
         cout << "Error:unable to create thread," << C0 << endl;
         exit(-1);
pthread_exit(NULL);
return 0;
}
}


something is very wrong.Thanks in advance
Last edited on
Topic archived. No new replies allowed.