views:

214

answers:

3

I am debugging an issue, where there is a thread which continues to run after the main thread has exited. It is stuck in a loop where it is waiting for another thread to change a variable.

I am trying to understand in what situations a process will continue running after the main thread has exited. I am using 32 bit linux g++ pthreads

A: 

This is really wird behaviour because only processes can be a zombie. Threads are placed in a memory of main program thread scope. Simplifies even detached threads are being killed by the system when the main thread finishes. Could You post a code?

bua
A: 

This is not a C++ question. Its about pthreads and the linux process mangement

Typically, process destruction occurs when the process calls the exit() system call, either explicitly when it is ready to terminate or implicitly on return from the main subroutine of any program (that is, the C compiler places a call to exit() after main() returns).

http://www.informit.com/articles/article.aspx?p=370047&seqNum=4

Having read that, i'd say the behavior you see is abnormal. I've seen similar things on windows. I don't believe its according to specifications. It rather is a bug of the OS, the Runtime libraries or whatever.

RED SOFT ADAIR
In a sense, it is a C++ question, since we are dealing with the static construction and destruction of global objects.
Juan
You did not mention that in your question. Thats no good practice. Please describe such important conditions in your questions.
RED SOFT ADAIR
I'm talking about C++ and not C. It should be implicitly understood that there is global construction and destruction of static objects since it is how the language works.
Juan
There of course is "global construction and destruction" in C++, but you did not mention that such objects are involved in your problem.
RED SOFT ADAIR
+2  A: 

If you exit from your main() function with pthread_exit() then the process will exit when your other thread(s) will finish. It is not abnormal in any way.

zacsek
Are there any additional situations that this might happen?
Juan
I do not know of any other situation that might cause this. However if you call exit() from one of your threads, then the whole process will exit just as if you had called return/exit() from your main() function.
zacsek
Another situation is if the main thread was canceled with `pthread_cancel()`. This cancels the one thread only; the others will continue running.
mark4o
You are right about that, but he wrote "after the main thread has exited", not "after i called pthread_exit()"
RED SOFT ADAIR
I don't know what is happening in the main thread to cause it to no longer exist. Sorry if the word "exit" was interpreted to mean that it is calling exit.All I know is that a stack trace on the main thread reveals it no longer exists.
Juan