I'm trying to get the concepts of returning a value from a pthread, and catching that value, but I cannot understand what is going on, or how to make it work. I have this simple program that creates a single thread, this thread exits with int value 100, and then I try to catch that value with pthread_join:
#include <stdio.h>
#include <pthread.h>
void *doStuff(void *param) {
int a = 100;
pthread_exit(a);
}
void main() {
pthread_t thread;
int a;
pthread_create(&thread, NULL, doStuff, NULL);
pthread_join(thread, &a);
printf("%d\n", a);
}
It works, but throws off a few warnings:
./teste.c: In function ‘doStuff’:
./teste.c:7:2: warning: passing argument 1 of ‘pthread_exit’ makes pointer from integer without a cast [enabled by default]
In file included from ./teste.c:2:0:
/usr/include/pthread.h:241:13: note: expected ‘void *’ but argument is of type ‘int’
./teste.c: In function ‘main’:
./teste.c:17:2: warning: passing argument 2 of ‘pthread_join’ from incompatible pointer type [enabled by default]
In file included from ./teste.c:2:0:
/usr/include/pthread.h:249:12: note: expected ‘void **’ but argument is of type ‘int *’
I don't understand why I'm getting those warnings, and honestly I don't understand why this is working either, because I thought I had to return a void pointer on pthread_exit.
This is a simple example that I'm trying to get to work so that I'm able to finish another program that I'm trying to create, in which I will have an array of threads, each of them calculating a single float value, and I want to store each of those calculated values into an array of floats using pthread_exit and pthread_join, but I don't really know how to store the values from pthread_join into an array.
int
is notvoid *
, perhaps? - user529758