C语言 将数组作为参数传递给 C 中的新线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22003083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing Array as argument to a new thread in C
提问by justin henricks
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[];
我正在尝试使用 pthread_create 将数组作为参数传递给新线程中的函数,这可能吗?我有一个整数数组和一个从 create thread 方法调用的计算平均值方法,但我似乎无法将数组正确传递到该方法中。这是我的代码: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);
}
回答by michaeltang
there is lots of problem in your code, i fix some to compile hope it will help
你的代码有很多问题,我修复了一些编译希望它会有所帮助
compile: gcc -o main main.c -lpthread
编译:gcc -o main main.c -lpthread
execute: ./main 2 5
执行:./main 2 5
output: 3
输出: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);
}
回答by user376507
Change the following
更改以下内容
void *calcAvg(int *nums[]){
int sum;
for(int i = 0; i < size; i++){
sum += nums[i];
}
average = sum / (size);
pthread_exit(0);
}
to
到
void *calcAvg(void *arg){
int *val_p = (int *) arg;
int sum;
for(int i = 0; i < size; i++){
sum += val_p[i];
}
average = sum / (size);
pthread_exit(0);
}
回答by Mark E. McDermott
The main issue that 'pthread_create()' takes a void pointer as its last argument. You are trying to pass to it an array of pointers to integers. Issue "man pthread_create" at the terminal to see the argument types you should be passing.
'pthread_create()' 将空指针作为其最后一个参数的主要问题。您正在尝试将指向整数的指针数组传递给它。在终端发出“man pthread_create”以查看您应该传递的参数类型。
What you really want to do is just pass the of array integers to the thread. In C, array indexing is just notation for pointer arithmetic. Writing nums[i]is equivalint to &nums[0] + ior just nums+i. The last case works because the name of an array in C can be used as a pointer to the first element of the array.
您真正想要做的只是将数组整数传递给线程。在 C 中,数组索引只是指针运算的符号。写作nums[i]相当于&nums[0] + i或只是nums+i。最后一种情况有效,因为 C 中数组的名称可以用作指向数组第一个元素的指针。
change void *calcAvg(int *nums[])to void *calcAvg(void* thread_args). Then in 'calcAvg' write int *nums = (int*)thread_args. Now you can use numsin that function just as if you had called calcAvg(nums), which in in essence you have done.
更改void *calcAvg(int *nums[])为void *calcAvg(void* thread_args). 然后在 'calcAvg' 中写入int *nums = (int*)thread_args. 现在您可以nums在该函数中使用,就像您调用了 一样calcAvg(nums),本质上您已经完成了。

