I'm working with pthreads in C and have run into problems(again). I am trying to send results as an array of a function to my main thread.
This code finds the largest value for each worker(1worker/row) and saves the indexes for that value. So far so good. I will explain my problem below this function. This code does what i want.
for (i = first; i <= last; i++){
for (j = 0; j < size; j++){
total += matrix[i][j];
if(matrix[i][j] > biggest){
biggest = matrix[i][j];
x=i;
y=j;
maxValueResult[1]=i;
maxValueResult[2]=j;
}else if(matrix[i][j] < smallest){
smallest = matrix[i][j];
indexes[2]=i;
indexes[3]=j;
}
}
}
maxValueResult[0]=biggest;
maxValueResult[1]=x;
maxValueResult[2]=y;
printf("Results: maxValue: %d cord[%d,%d]\n",maxValueResult[0],maxValueResult[1],maxValueResult[2]);
return *maxValueResult;
My problem is when i recive the results in pthread_join only maxValueResult[0] works, from maxValueResult[1] and maxValueResult[2] i only get addresses or something. before: " return *maxValueResult;" all three values are correct, looking something like this: "Results: maxValue: 95 cord[3,3]"
Here is where i call pthread_join:
for(l=0;l<numWorkers;l++){
pthread_join(workerid[l], &maxValue);
temp[l] = maxValue[0];
if(maxValue[0]<temp[1]){
maxValue[0] = temp[1];
xCORD = maxValue[1];
yCORD = maxValue[2];
}
}
printf("Results: maxValue: %d cord[%d,%d]\n",maxValue[0],xCORD,yCORD);
That prints this: "Results: maxValue: 95 cord[0,1629976061]", where 95 is correct but not the other two. What can I do to fix this? As you probably can see from my code, im not very experienced.
EDIT: I forgot to tell about the warnings, i get a couple! Unfortunatly they are in swedish, but i post them here anyway: "$ gcc -o sumb matrixSumB.c matrixSumB.c: In funkcion "main": matrixSumB.c:120:1: warning: passing argument 2 of ‘pthread_join’ from incompatible pointer type /usr/include/pthread.h:144:5: note: expected ‘void **’ but argument is of type ‘int * (*)[3]’ matrixSumB.c: In function "Worker": matrixSumB.c:172:1: warning: return makes pointer from integer without a cast. "
Kind Regards Leo
return *maxValueResult;
returns only the first value in themaxValueResult
array, and not a pointer to the array. And of course, returning pointers to local variables is a big no-no. – Some programmer dude