gpt4 book ai didi

C:multithreading-父线程在输出前退出

转载 作者:太空宇宙 更新时间:2023-11-04 08:03:57 24 4
gpt4 key购买 nike

更新包含在第一个代码块之后

我是多线程的新手,在 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);
}

最佳答案

您没有正确启动线程。您必须传递将执行的函数,但您实际上是在调用您的函数并将其返回值传递给 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);
...
}

同样适用于所有其他线程。您遇到的其他问题:

  • 你的线程函数(pthreads 期望的)应该接受一个 void 指针并返回 void 指针。例如:void *threadFunc(void *nums)
  • 你的函数应该返回一些东西(例如return NULL;)
  • 你不应该尝试连接你自己的线程:pthread_join(pthread_self(),NULL);

If anyone can tell me if I can keep: int average,maximum,minimum inside *parentThread and still calculate them inside the child threads

当然可以。定义一些存储所有数据的结构并将指向该数据的指针传递给您的线程:

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;
}

关于C:multithreading-父线程在输出前退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44059128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com