pthreads - About the parameter of function pthread_create? -
we know call pthread this:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void* arg); hi guys, want know why return type of third parameter void*? why not void?
because there no way start function know kind of data developer wants return function use void* can point type. developer of start function cast void* appropriate type returned before using whatever void* points to. start function can return pointer may in point anything. if start function declared return void, means function returns nothing, if developer wants start function return int, struct? example:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <pthread.h> struct test { char str[32]; int x; }; void *func(void*) { struct test *eg = (struct test *)malloc(sizeof(struct test)); strcpy(eg->str,"hello world"); eg->x = 42; pthread_exit(eg); } int main (void) { pthread_t id; struct test *resp; pthread_create(&id, null, func, null); pthread_join(id,(void**)&resp); printf("%s %d\n",resp->str,resp->x); free(resp); return 0; } more details on post: what void* mean , how use it?
Comments
Post a Comment