Example ( source ):
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
The function is declared to return a void pointer, but there's no return statement in its body. What is going on here?
Generally if a function has been declared as returning a value (in this case, a void pointer) and there's no return statement, its behaviour is undefined. Comments say that pthread_exit(NULL)
is equivalent for calling return NULL
- how can it be? You can call function B()
from A()
in such a way that B()
calls return for A
. Return statements refer to the function in which they are placed.
We can only speculate about what may be going on, but since it looks like a "Hello, World!" program, a reasonable guess would be that a pointer to that function is intended to be passed to pthread_create()
. pthread_create()
requires a pointer to a function having that signature. It's a strange construction even so; it would be equivalent but clearer for the function to just return NULL
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.