简体   繁体   中英

incorrect return value of pthread_join

I trying to find the minimum value of the array, I split the array and with thread, I use in function to find the minimum, in the final I try to get the minimum value of each sub-array and between this value to find the final minimum value but in the final, I get incorrect value.

for example: @ubuntu:~$ gcc -pthread -o hw3 hw3.c @ubuntu:~$./hw3 1 2 3 4 5 1 2 Min = 1 3 4 Min = 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;
}

Your code has some issues:

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

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

In this case, sizeof(s) = sizeof(int*) = 8 and sizeof(s[0]) = sizeof(int) = 4. You can't compute the length of an array like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM