简体   繁体   English

如何使用pthreads在c中返回数组?

[英]How to return an array in c using pthreads?

I tried to write a program that a thread returns an array of numbers by passing a random vector to the thread and thread returns 2 times of the vector. 我试图编写一个程序,该程序通过将随机向量传递给线程来使线程返回数字数组,并且线程返回向量的2倍。 The program is running fine put I'm not getting the expected output. 程序运行良好,没有得到预期的输出。 The code is as follows: 代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <omp.h>
void *myThreaddouble();

int i,sum[10],first[10]={10,20,30,40,50};

void *myThreaddouble()
{

int *sum[10];
for(i=0;i<5;i++)
{
  sum[i]= first[i] + first[i];
}    

   pthread_exit(sum[i]);
}

int main()
{

 double total = omp_get_wtime();

 printf("This is using PThread\n");

  pthread_t tid1;
  pthread_create(&tid1, NULL, myThreaddouble, NULL);
  printf("Double of the vector:-   \n");
  for ( i = 0 ; i < 5 ; i++ )
  {
    printf(" %d\n",(int) sum[i]);
  }
  pthread_join(tid1, sum[i]);

  total = omp_get_wtime() - total;
  printf("%lf is time to add\n",total);
  printf("\n");

  return 0;

}

and the output is as follows: 输出如下:

This is using PThread
Double of the vector:-   
 0
 0
 0
 0
 0
0.000188 is time to add

Which is not the expected output, so can someone tell me what is the mistake in this code and how to return an array using pthread. 这不是预期的输出,因此有人可以告诉我此代码中的错误是什么以及如何使用pthread返回数组。 In the i should be able to sum all the return values. 在i中,我应该能够对所有返回值求和。

I used gcc qc -lpthread -lrt -fopenmp command to compile this program. 我使用gcc qc -lpthread -lrt -fopenmp命令来编译该程序。

Thank you in advance! 先感谢您!

Probably the problem is being caused because of the line int *sum[10]; 可能是由于int *sum[10];这一行引起的int *sum[10]; in this function: 在此功能中:

void *myThreaddouble()
{

int *sum[10];
for(i=0;i<5;i++)
{
  sum[i]= first[i] + first[i];
}

You are creating a local variable sum , which will have preference over the global variable sum , since it is in a inner scope. 您正在创建一个局部变量sum ,它比全局变量sum优先,因为它在内部范围内。 Try removing it. 尝试将其删除。

There you go. 妳去 I have taken liberty to comment out the time calculation part. 我已自由注释掉时间计算部分。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <omp.h>
void *myThreaddouble();

int i,sum[10],first[10]={10,20,30,40,50};

void *myThreaddouble(void *a)
{

//int *sum[10];
for(i=0;i<5;i++)
{
  sum[i]= first[i] + first[i];
}    

   //pthread_exit(sum[i]);
   pthread_exit(NULL);
}

int main()
{

// double total = omp_get_wtime();

 printf("This is using PThread\n");

  pthread_t tid1;
  pthread_create(&tid1, NULL, myThreaddouble, NULL);
  printf("Double of the vector:-   \n");

  //pthread_join(tid1, sum[i]);
  pthread_join(tid1, NULL);

  for ( i = 0 ; i < 5 ; i++ )
  {
    printf(" %d\n",(int) sum[i]);
  }
 // pthread_join(tid1, sum[i]);

  //total = omp_get_wtime() - total;
  //printf("%lf is time to add\n",total);
  printf("\n");

  return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM