【问题标题】:incorrect return value of pthread_joinpthread_join 的返回值不正确
【发布时间】:2021-10-26 14:29:11
【问题描述】:

我试图找到数组的最小值,我拆分数组并使用线程,我在函数中使用找到最小值,最后我尝试获取每个子数组的最小值以及该值之间的最小值找到最终的最小值,但在最后,我得到了不正确的值。

例如: @ubuntu:~$ gcc -pthread -o hw3 hw3.c @ubuntu:~$ ./hw3 1 2 3 4 5 1 2 最小值 = 1 3 4 最小值 = 3 Min =[0]linoy1@ubgedit hw3.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>


int min_value(int a[], int n) 
    {
    int c, index = 0;
        for (c = 1; c < n; c++)
            if (a[c] < a[index])
                index = c;

    return a[index];
    }


void* Find_min_val(void* arg)
    {       
        int *s;
        int temp;
        s=(int*) arg;
        int numOfarray=sizeof(s)/sizeof(s[0]);
        int array[numOfarray];

        for(int j=0; j<numOfarray; ++j) 
        {
            array[j]=s[j];
            printf("%d\n",array[j]);
        }

        temp=min_value(array,numOfarray);  
            printf("Min = %d\n", temp);
        //*(int *) arg = s;
        void *Vtemp;        
        Vtemp=&temp;
        return Vtemp;

    }

int main(int argc, char const *argv[])
    {
    int j, element,num=argc,arr[argc-1],min = arr[0];
    int halfArgc1,halfArgc2,k= argc % 2;
    pthread_t tid1,tid2;

    //argc input
      if(argc == 1)
        perror("Not enough arguments entered\n");


    //argc / 2

        halfArgc1=argc/2;
        if(k=0){//argc even
            halfArgc2=halfArgc2;
        }
        else    {//argc odd         
            halfArgc2=halfArgc1+1;
        }

    //input argv[argc]          
      
        
          for(j= 1; j < argc; j++)
        {
        element = atoi(argv[j]);
        arr[j-1] = element;
        }

        int subArr1[halfArgc1],subArr2[halfArgc2];
        memcpy(subArr1,arr,halfArgc1*sizeof(int));
        memcpy(subArr2,arr+halfArgc1-1,halfArgc2*sizeof(int));

    


    if (pthread_create(&tid1, NULL, Find_min_val,(void*)subArr1))
        return 1;

    if (pthread_create(&tid2, NULL, Find_min_val,(void*)subArr2))
        return 1;
    
    int* r;

    if (pthread_join(tid1,(void**) &r))
    {
        fprintf(stderr,"Could not join Foo Thread\n");
        return 1;
    } 
    int Min_Val[2];
    Min_Val[1]=*(int*)r;
    if (pthread_join(tid2,  (void**) &r))
    {
        fprintf(stderr,"Could not join with Bar Thread\n");
        return 1;
    }
    Min_Val[1]=*(int*)r;
    printf("Min =[%d]",min_value(Min_Val,2));


    return 0;
}

【问题讨论】:

  • 鉴于Vtemp=&amp;temp; return Vtemp;,当Find_min_val() 返回时,您认为temp 变量会发生什么?

标签: c unix


【解决方案1】:

您的代码有一些问题:

if(k=0) // --> raised a warning at compilation. Should be : if (k == 0)

int numOfarray=sizeof(s)/sizeof(s[0]);

在这种情况下,sizeof(s) = sizeof(int*) = 8 和 sizeof(s[0]) = sizeof(int) = 4。 你不能像这样计算数组的长度。

【讨论】:

  • C 标准允许if(k=0),如果满足其他要求(例如k 被声明为可与0 比较的类型),则符合标准的编译器必须接受它。如果您的编译器不接受它,那么它就处于非一致性模式(这可能就像将警告提升为错误的模式一样简单)。说它不编译是不正确的,因为代码使用 Apple Clang 11 和默认选项为我编译。
  • 我不是 OP,也不想写 if(k=0)if (k == 0)。我只是指出答案中的断言是不正确的。答案不应提供虚假信息。您可以对其进行编辑以修复它。
猜你喜欢
  • 2017-11-04
  • 2012-10-30
  • 2020-08-15
  • 2016-10-09
  • 2015-10-13
  • 2014-12-06
  • 2021-02-01
  • 2017-08-21
  • 2021-10-27
相关资源
最近更新 更多