A pointer to a concrete data type, converted to void *
, can be used to pass values to and return results from the thread function.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct thread_args
{
int a;
double b;
};
struct thread_result
{
long x;
double y;
};
void *thread_func(void *args_void)
{
struct thread_args *args = args_void;
/* The thread cannot return a pointer to a local variable */
struct thread_result *res = malloc(sizeof *res);
res->x = 10 + args->a;
res->y = args->a * args->b;
return res;
}
int main()
{
pthread_t threadL;
struct thread_args in = { .a = 10, .b = 3.141592653 };
void *out_void;
struct thread_result *out;
pthread_create(&threadL, NULL, thread_func, &in);
pthread_join(threadL, &out_void);
out = out_void;
printf("out -> x = %ld\tout -> b = %f\n", out->x, out->y);
free(out);
return 0;
}
In many cases it is unnecessary to pass a return value in this way - for example, space in the argument struct can also be used to return results, or a pointer to a shared data structure can be passed to the thread and the results stored there.