(1) In C++ code, using return
causes the stack to be unwound and local variables destroyed, whereas pthread_exit
is only guaranteed to invoke cancellation handlers registered with pthread_cancel_push()
. On some systems this mechanism will also cause the destructors for C++ local variables to be called, but this is not guaranteed for portable code --- check your platform documentation.
Also, in main()
, return
will implicitly call exit()
, and thus terminate the program, whereas pthread_exit()
will merely terminate the thread, and the program will remain running until all threads have terminated or some thread calls exit()
, abort()
or another function that terminates the program.
(2) The use of return
works because the POSIX specification says so. The returned value is stored in a place where pthread_join()
can retrieve it. The resources used by the thread are not reclaimed until pthread_join()
is called.
(3) I never use the return value of a thread in raw POSIX threads. However, I tend to use higher level facilities such as the Boost thread library, and more recently the C++0x thread library, which provide alternative means for transferring values between threads such as futures, which avoid the problems associated with memory management that you allude to.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…