delphij's Chaos

选择chaos这个词是因为~~实在很难找到一个更合适的词来形容这儿了……

26 Oct 2005

Linux threading bug

A collegue of mine has reported a FreeBSD “bug” that, when the main thread exits, the whole process is terminated. He complained that pthread_detach() would not cause the thread to run without the main thread, while Linux did.

With some experiments we have figured out that this is actually a Linux bug, which violates POSIX. Interestingly, Solaris has the same behavior that FreeBSD have.

The POSIX pthread_create() said:

_"Note that the thread in which main() was originally invokeddiffers from this. When it returns from main(), the effectshall be as if there was an implicit call to exit() usingthe return value of main() as the exit status."_

And, the exit() said:

_"These functions shall terminate the calling process"_

Consequently, the termination of the main thread should cause the whole process to terminate.

In order to keep other threads running, one must call pthread_join().

BTW. This pointed out that the text found in Unix Network Programming, 3rd Edition is wrong about POSIX threads.