0

Running the following C code causes the program to hang, and does not respond to signals (including CTRL-C).

int main()
{
    pthread_exit(0);
    return 0;
}

Any idea why?

The behaviour is normal when other threads have been created and are running, but I would like to know if I always have to check that before using pthread_exit(0).

EDIT: This is the complete code that hangs. However, I was building with glib (-lglib-2.0). Using simply cc -o foo foo.c works as expected.

ralux
  • 25
  • 5

1 Answers1

0

Your entire use case is described in the notes of the pthread_exit man page. In your case, as you correctly edited your OP, glib started another thread. You exited the main thread and the other thread kept running. You labeled this as a hang. In general, if you want to exit the application in full, just use exit or return from main().

Only when you need additional magic (rarely) like detached threads, use pthread_exit() on the main thread.

Klaas van Gend
  • 1,105
  • 8
  • 22
  • 1
    I don't think the man page says anything related to the behaviour described by OP. I can't reproduce the behaviour with glib. Why would glib start another thread? – P.P May 19 '16 at 13:19