When I call pthread_exit from main, the program never gets to terminate. I expected the program to finish, since I was exiting the program’s only thread, but it doesn’t work. It seems hung.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int main(int argc, char *argv[])
{
printf("-one-\n");
pthread_exit(NULL);
printf("-two-\n");
}
Process Explorer shows that the (only) thread is in Wait:DelayExecution state.
According to pthread_exit documentation:
The process shall exit with an exit
status of 0 after the last thread has
been terminated. The behavior shall be
as if the implementation called
exit() with a zero argument at thread
termination time.
I’m using Dev-C++ v4.9.9.2 and pthreads-win32 v2.8.0.0 (linking against libpthreadGC2.a).
The library seems to be OK (for example, calling pthread_self or pthread_create from main works fine).
Is there any reason for what I’m not supposed to call pthread_exit from main?
Well its definately legal in the linux implementation of pthreads, see the notes section in pthreads_exit. It states
Further, a look at the source code here (torwads the end) shows that it roughly translates to _endthread or _endthreadex. The documentation here for those makes no mention of not calling it in the initial thread.