What happens if you call exit(0) while other threads are still running?

I'm answering the question in the title of your question, not the 3 bullet points, because I think the answers to the bullet point questions are irrelevant to answer the actual question.

Using exit when the program is in a random state - as you seem to suggest - is usually a rather brutal and undeterministic way to end a program even with a single thread. It doesn't even matter if the thread gets destroyed before object destruction or after, both ways result in nightmares. Remember, that each thread could be in a random state and accessing anything. And the stack objects of each thread will not be destroyed properly.

See the documentation of exit to see what it does and doesn't clean up.

The favored way I've seen to correctly shutdown a multithreaded program, is to make sure no thread is in a random state. Stop all the threads in some way or another, call a join on them where feasible, and from the last remaining thread call exit - or return if this happens in the main function.

An incorrect approach I've seen often is to correctly dispose of some objects, close some handles, and generally try to do a proper shutdown until everything goes wrong, and then call terminate. I advise against that.


Let me give a try to answer your questions. Guys, correct me if i am going wrong.

Your program is crashing occasionally. This is the expected behavior. You have released all of the acquired resources. And your thread, which is alive is trying to access the resources, based on the information it has. If it is successful, it will run. If it isn't successful, it would crash.

Often the behavior would be sporadic. If the OS allocates the released resources to other processes, or if it uses the resources, then you would see your thread crashing. If not, your thread runs. This behavior is dependent on OS, Hardware, RAM, % of resources being utilized when the process died. Any over use of resources, etc, etc.

Does the thread get killed prior to the C++ object destruction? No. C++ doesn't have any inbuilt support for threads. P threads are just posix threads, which work with the underlying OS and provide you a functionality to create threads, if required. Technically, As threads are not a part of C++, the threads getting killed automatically is not possible. Correct me if i am wrong.

Is C++ is not aware of the threads, so these keep running until the C++ cleanup is complete? C++ is not aware of the threads. The same cannot be said for C++11

Should the SIGTERM handler first shutdown or kill the threads before proceeding, or does this happen automatically? Technically SIGTERM handler shouldn't kill the threads. Why do you want the OS handlers to kill the running threads? Every operating system works on the hardware to provide the functionality to the users. Not to kill any of the running processes. Well, Programmers must join the threads to the main, but there might be some cases where you want to let your threads run for some time. May be.

It is the software developer/vendor responsibility to write code which doesn't crash or end up in infinite loops, and to kill all running threads when required. OS cannot take the responsibility of these acts. This is the reason why, Windows/Apple certifies some softwares for their OS's. So, customers can buy that with peace of mind.

Tags:

C++

Pthreads