Reputation: 5993
I have a multi-threaded C++03 application (Linux-based) that I want to terminate immediately in a thread-safe manner.
I have tried using exit(0) but this destroys some static variables which are actively being used by another thread, causing that thread to access freed memory and destroyed objects, resulting in a core dump! Apparently the exit() function has a data race: "Calling this function destroys all objects with static duration: A program with multiple threads running shall not call exit (see quick_exit for a similar function that does not affect static objects)."
C++11 offers a thread-safe quick_exit() function. But I don't have the ability to move this large application to C++11 at the moment.
I also don't want to spend effort trying to do clean termination/joining of threads. This is a very complex program and that would take a considerable amount of work.
Are there any other alternatives? I just want the program to exit immediately, no cleanup, no core dump.
Edit: What I'm really trying to do is replace abort() calls with something that won't create a coredump. And abort() is thread-safe, btw.
Upvotes: 1
Views: 2246
Reputation: 6224
It is not clear what you want to terminate: process or thread. It's usually safe to terminate a process and return control to OS but terminating a thread by "brute force" is usually a bad idea. The reason is many code and library are written with the assumption of being running in a single threaded environment. Killing a thread WILL leave some global variables in a "quantum state", which will cause problems for the code running in other threads. It's better to pay your effort to find some place in the said code and politely "ask" it exit.
Upvotes: 0
Reputation: 69912
You will get the effect you want by calling _exit(status)
(note the leading underscore)
documentation:
http://man7.org/linux/man-pages/man2/_exit.2.html
Upvotes: 3