Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

pthreads

Suppose a program has several threads: t1, t2, etc. These are using pthreads. The t2 thread is sitting in a loop reading from a stream and accessing a variable with static storage duration.

Now suppose t1 calls exit(0).

(Further details: I have a program doing this on a Unix-based system, and is compiled with g++. The program appears to occasionally be crashing on shutdown with a stack trace that indicates the static variable is not valid.)

  • Does the thread get killed prior to the C++ object destruction?

  • Is C++ not aware of the threads, so these keep running until the C++ cleanup is complete?

  • Should the SIGTERM handler first shutdown or kill the threads before proceeding, or does this happen automatically?

like image 445
mbells Avatar asked Nov 07 '14 17:11

mbells


People also ask

Does exit () terminate all threads?

Calling the exit subroutine terminates the entire process, including all its threads. In a multithreaded program, the exit subroutine should only be used when the entire process needs to be terminated; for example, in the case of an unrecoverable error.

What happens when two threads call the same function?

There is nothing wrong in calling same function from different threads. If you want to ensure that your variables are consistent it is advisable to provide thread synchronization mechanisms to prevent crashes, racearound conditions.

What happens to a detached thread when main () exits?

The answer to the original question "what happens to a detached thread when main() exits" is: It continues running (because the standard doesn't say it is stopped), and that's well-defined, as long as it touches neither (automatic|thread_local) variables of other threads nor static objects.

How do you end all threads in C++?

In short: You can't kill threads in ISO c++. You can only e.g. repeatedly poll a flag inside the thread and stop further processing. As std::threads are usually implemented on top of pthreads on linux, you might be able to leverage that API, e.g. have a look at pthread_cancel.


2 Answers

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.

like image 104
Peter Avatar answered Nov 15 '22 08:11

Peter


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.

like image 34
kris123456 Avatar answered Nov 15 '22 07:11

kris123456