In c, we create a thread like so:
void * run(void * arg){
printf("hello world\n");
}
int main(){
pthread_t thread;
int a = pthread_create(&thread, NULL, run, (void*)0);
}
But it will not work if I declare run as
void run(){}
On the other hand, if I cast it to (void *)
in the parameter of pthread_create
, it works fine. So it only accepts functions with return types of (void *)
.
Why?
Thanks !
The thread function must be declared to return void *
because the threading library expects such a return value, and will store it into a location given to pthread_join()
after the thread terminates.
If you don't need the thread return value for anything, you can just return 0;
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With