This question comes up in my mind when I look at the synopsis of pthread_create, which is:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
I don’t understand why the third parameter has to be that complex. Its purpose is just for passing in a function address. So why don’t they use
void (*start_routine)(void *)
or even
void (start_routine)(void *)
In general, in what situations should we use a function signature like the third parameter above?
The start_routine takes a
void*to allow the thread starter to pass some arbitrary data to the new thread. It returns avoid*so that the thread can return a meaningful exit code to the caller (instead of explicitly having to invokepthread_exitfrom the thread function).Note that your second suggestion (
void (start_routine)(void*)is not a function pointer, and so isn’t an option for a parameter topthread_createor any other function at all.