传递数组作为参数在C一个新的线程 [英] Passing Array as argument to a new thread in C
本文介绍了传递数组作为参数在C一个新的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图数组作为参数传递给函数使用pthread_create的一个新的线程,这可能吗?我有一个整数数组,并且从创建线程方法调用一个计算平均的方法,但我似乎无法给我的数组正确地传递到方法。这里是我的code:
INT NUMS [];
INT平均水平;INT大小= 0;
无效* calcAvg为(int * NUMS []);INT主(INT ARGC,CHAR *的argv []){
/ *初始化整数数组传递* /
NUMS [ARGC - 1];
的for(int i = 0; I< ARGC - 1;我++){
NUMS [I] =的atoi(argv的[I + 1]);
大小++;
} / *线程标识符* /
的pthread_t avgThread; 在pthread_create(安培; avgThread,NULL,calcAvg,NUMS); 在pthread_join(avgThread,NULL); 的printf(平均=%d个,平均);
}无效* calcAvg为(int * NUMS []){
INT总和;
的for(int i = 0; I<大小;我++){
总和+ = NUMS [I]
}
平均=总和/(大小);
了pthread_exit(0);
}
解决方案
有大量的问题,在你的code,我修正了一些编译
希望这将有助于
编译:gcc的-o主要的main.c -lpthread
执行:./main 2 5
输出:3
的#include< pthreads.h中>
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT平均水平;INT大小= 0;
无效* calcAvg(无效* ARG);
INT主(INT ARGC,CHAR *的argv []){
/ *初始化整数数组传递* /
为int * NUMS =(INT *)malloc的((ARGC - 1)* sizeof的(INT));
INT I = 1;
对于(i = 1; I< ARGC,我++){
NUMS [I-1] =与atoi(argv的[I]);
大小++;
} / *线程标识符* /
的pthread_t avgThread; 在pthread_create(安培; avgThread,NULL,calcAvg,(无效*)NUMS); 在pthread_join(avgThread,NULL);
的printf(平均=%d个\\ N,平均);
免费(NUMS);}
无效* calcAvg(无效* ARG){
为int * val_p =(INT *)ARG;
INT总和= 0;
INT I = 0;
对于(i = 0; I<大小;我++){
总和+ = val_p [I]
}
平均=总和/(大小);
了pthread_exit(0);
}
I am attempting to pass an array as an argument to a function in a new thread using pthread_create, is this possible? I have an array of integers and a calculate average method that is called from the create thread method but I cannot seem to pass my array into the method correctly. Here is my code: int nums[];
int average;
int size = 0;
void *calcAvg(int *nums[]);
int main(int argc, char *argv[]){
/* initialize an array of the integers to be passed */
nums[argc - 1];
for(int i = 0; i < argc - 1; i++){
nums[i] = atoi(argv[i + 1]);
size++;
}
/* Thread Identifier */
pthread_t avgThread;
pthread_create(&avgThread, NULL, calcAvg, nums);
pthread_join(avgThread, NULL);
printf("average= %d", average);
}
void *calcAvg(int *nums[]){
int sum;
for(int i = 0; i < size; i++){
sum += nums[i];
}
average = sum / (size);
pthread_exit(0);
}
解决方案
there is lots of problem in your code, i fix some to compile hope it will help
compile: gcc -o main main.c -lpthread
execute: ./main 2 5
output: 3
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int average;
int size = 0;
void *calcAvg(void *arg);
int main(int argc, char *argv[]){
/* initialize an array of the integers to be passed */
int *nums = (int*)malloc((argc - 1)*sizeof(int));
int i = 1;
for(i = 1; i < argc ; i++){
nums[i-1] = atoi(argv[i]);
size++;
}
/* Thread Identifier */
pthread_t avgThread;
pthread_create(&avgThread, NULL, calcAvg, (void*)nums);
pthread_join(avgThread, NULL);
printf("average = %d \n",average);
free(nums);
}
void *calcAvg(void *arg){
int *val_p = (int *) arg;
int sum = 0;
int i = 0;
for( i = 0; i < size; i++){
sum += val_p[i];
}
average = sum / (size);
pthread_exit(0);
}
这篇关于传递数组作为参数在C一个新的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文