在第一个代码块
之后包含更新我是多线程新手,在输出C程序中各种线程的结果时遇到问题。具体来说,我试图使用不同的线程为用户输出整数数组中的平均值,最大值和最小值。但是,应该打印三个值的父线程在完成其子线程时终止。
为了测试,我目前在子线程中打印结果,但我需要在父线程中打印这些值。代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void *avgThread(int *nums,int*out)
{
//sleep(1);
int total = 0,x,avg;
for(x=0;x<7;x++) total+=nums[x];
avg = total/7;
*out = avg;
printf("%d\n",avg);
}
void *minThread(int *nums,int*out)
{
int min = nums[0],x;
for(x=1;x<7;x++) if(nums[x]<min) min=nums[x];
*out = min;
printf("%d\n",min);
}
void *maxThread(int *nums,int*out)
{
int max = nums[0],x;
for(x=1;x<7;x++) if(nums[x]>max) max=nums[x];
*out = max;
printf("%d\n",max);
}
void *parentThread(int*nums)
{
int average,minimum,maximum;
pthread_t avg,min,max;
pthread_attr_t avgfun,minfun,maxfun;
pthread_attr_init(&avgfun);
pthread_attr_init(&minfun);
pthread_attr_init(&maxfun);
pthread_create(&avg, &avgfun, avgThread(nums,&average), NULL);
pthread_create(&min, &minfun, minThread(nums,&minimum), NULL);
pthread_create(&max, &maxfun, maxThread(nums,&maximum), NULL);
pthread_join(avg, NULL);
pthread_join(min, NULL);
pthread_join(max, NULL);
printf("%d\n",maximum);
printf("%d\n",average);
printf("%d\n",minimum);
pthread_join(pthread_self(),NULL);
return NULL;
}
int main()
{
int nums[] = {90, 81, 78, 95, 79, 72, 85};
pthread_t prnt;
pthread_attr_t parent;
pthread_attr_init(&parent);
pthread_create(&prnt,&parent,parentThread(nums),NULL);
pthread_join(prnt,NULL);
sleep(3);
exit(0);
}
更新
感谢Pavel和其他人指出我做错的一些事情。我已经对线程初始化和调用进行了必要的修改。我也能够在父线程中输出计算的平均值,最大值和最小值,但只能在范围内使所述变量全局化。如果有人能告诉我我是否可以保留:
int average,maximum,minimum
在* parentThread内部,仍然在子线程内计算它们,这将是很棒的。这是我现在的代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
int average,minimum,maximum;
void *avgThread(void *numsptr)
{
sleep(1);
int *nums=(int*)numsptr;
int total = 0,x;
for(x=0;x<7;x++) total+=(int)nums[x];
average = total/7;
return NULL;
}
void *minThread(void *numsptr)
{
sleep(2);
int *nums=(int*)numsptr,x;
minimum = nums[0];
for(x=1;x<7;x++) if((int)nums[x]<minimum) minimum=(int)nums[x];
return NULL;
}
void *maxThread(void *numsptr)
{
sleep(3);
int *nums=(int*)numsptr,x;
maximum = nums[0];
for(x=1;x<7;x++) if((int)nums[x]>maximum) maximum=(int)nums[x];
return NULL;
}
void *parentThread(void *numsptr){
int *nums=(int*)numsptr;
//int average,minimum,maximum;
pthread_t avg,min,max;
pthread_attr_t avgfun,minfun,maxfun;
pthread_attr_init(&avgfun);
pthread_attr_init(&minfun);
pthread_attr_init(&maxfun);
pthread_create(&avg, &avgfun, &avgThread,(void*)nums);
pthread_create(&min, &minfun, &minThread,(void*)nums);
pthread_create(&max, &maxfun, &maxThread,(void*)nums);
pthread_join(avg, NULL);
printf("The average value is %d\n",average);
pthread_join(min, NULL);
printf("The minimum value is %d\n",minimum);
pthread_join(max, NULL);
printf("The max value is %d\n",maximum);
return NULL;
}
int main()
{
int nums[] = {90, 81, 78, 95, 79, 72, 85};
pthread_t prnt;
pthread_attr_t parent;
pthread_attr_init(&parent);
pthread_create(&prnt,&parent,&parentThread,(void*)nums);
pthread_join(prnt,NULL);
exit(0);
}
答案 0 :(得分:2)
您没有正确启动线程。你必须传递将要执行的函数,但实际上是在调用你的函数并将其返回值传递给pthread_create
:
// call parentThread(nums) and then create thread.
pthread_create(&prnt, &parent, parentThread(nums),NULL);
你想要做的是:
void *parentThread(void *nums_ptr)
{
int *nums = (int*)nums_ptr;
...
return NULL;
}
int main()
{
int nums[] = {90, 81, 78, 95, 79, 72, 85};
pthread_t prnt;
pthread_create(&prnt, NULL, &parentThread, (void*)nums);
...
}
同样适用于所有其他线程。 您遇到的其他问题:
void *threadFunc(void *nums)
return NULL;
)pthread_join(pthread_self(),NULL);
如果有人能告诉我是否可以保留:
int average,maximum,minimum
在* parentThread中,仍然在子线程内计算它们
当然可以这样做。定义一些存储所有数据的结构,并将指向该数据的指针传递给您的线程:
struct myState
{
int average, minimum, maximum;
int *nums
};
void *avgThread(void *data)
{
sleep(1);
myState *state = (myState*)data;
int total = 0,x;
for(int x=0; x<7; x++)
total += state->nums[x];
state->average = total/7;
return NULL;
}
void *parentThread(void *nums_ptr)
{
int *nums = (int*)nums_ptr;
myState state;
state.nums = nums;
...
pthread_create(&avg, &avgfun, &avgThread, &state);
...
pthread_join(avg, NULL);
printf("The average value is %d\n", state.average);
...
return NULL;
}