如何从main访问线程中完成的计算?

时间:2018-12-24 00:15:15

标签: c multithreading pi

我正在练习创建3个线程并计算Leibniz系列的一部分来计算pi。一切正常,但是我无法访问线程中将main中的3个数字相加的计算结果。有没有人以我退货的方式看到问题?

DRIVER.c:

/*Headers in evey file are this*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "driver.h"
#define thread_number 3

static float Pi = 3.141592;

void print_result(float * sums, float Final_pi, float error){
  int i;

  for(i = 1; i <= thread_number; i++)
   printf("Sum done by the thread number %i: %f\n", i, *(sums + i - 1));

  printf("The real PI: %f\n", Pi);
  printf("PI computed by the program: %f\n", Final_pi);
  printf("Error, aka difference between the two Pi above: %f\n", error);
}



int main(int argc, char** argv){   
  int i;/*variable for loops*/
  int iterations = atoi(argv[1]);/*number of iterations given by the user*/
  int error;
  float sum[thread_number] = {0.0f};
  data_struct args[thread_number];
  pthread_t thread_id[thread_number];
  float Final_pi = 0.0f;
  void* returns[3];


  if(argc == 0){
    printf("No arguments supplied\n");
    return -1;
  }

  if(argc > 2){
    printf("There are too many arguments\n");
    return -1;
  }


  args[0].mIterations = iterations;
  args[0].mFirst_value = 1;
  args[1].mIterations = iterations;
  args[1].mFirst_value = 2;
  args[2].mIterations = iterations;
  args[2].mFirst_value = 3;
  args[0].mSum = sum;
  args[1].mSum = sum+1;
  args[2].mSum = sum+2;



  for(i = 0; i < thread_number; i++){
    pthread_create(&thread_id[i], NULL, ComputePi, &args[i]);
    args[i].mFirst_value = i + 1;
    args[i].mIterations = iterations;
  }

  for(i = 0; i < thread_number; i++){
    pthread_join(thread_id[i], &returns[i]);
    args[i].mSum = (float*)returns[i];
    printf("%f\n",*args[i].mSum);
  }


/*Add the computations together*/
  for(i = 0; i < thread_number; i++)
    Final_pi += *args[i].mSum;


  error = Pi - Final_pi;

  print_result(sum, Final_pi, error);

  return 0;
}


pi.c:
/*code in threads*/

void* ComputePi(void * data){
  int i;
  int sign = 1;
  int denominator = 1;
  float sum = 0.0f;
  data_struct * args = (data_struct *)data;


  if(args->mFirst_value == 1){
    sign = 1;/*first number of the series*/
    denominator = 1;
  }
  else if (args->mFirst_value == 2){
    sign = (-1);/*second number of the series*/
    denominator = 3;
  }
  else if(args->mFirst_value == 3){
    sign = 1;/*Third number of the series*/
    denominator = 5;
  }

  /*loop to compute one of the 3 numbers that added together give us Pi*/
  for(i = args->mFirst_value; i <= args->mIterations; i = i+3){
    sum +=  sign * 4.0f / denominator;
    denominator += 6;
    sign *= -1;         
  }

  args->mSum = &sum;
  printf("mSum %f\n",*args->mSum);

  return (void*)args->mSum;
 }

driver.h:
void* ComputePi(void * data);

/*data_struct struct*/
typedef struct{
float * mSum;
int mIterations;
int mFirst_value;
}data_struct;

该线程中的printf给出了实际结果,但main中的printf仅打印0 3次。

1 个答案:

答案 0 :(得分:1)

mSum是指向float指针

它被设置为sum stack 帧中的ComputePi变量的地址。当ComputePi退出/返回时,这超出范围。

mSum更改为float mSum并执行args->mSum = sum而不是args->mSum = &sum

然后,只需在args[i].mSum中打印main

请注意,还有其他组织方式,但这是最简单的方式。