c++ - How to override exit(), perhaps by throwing exception -
We have a third party library which was kept in mind without multithreading or exception handling. Our main performance is usable and uses exceptions.
The third party library uses the exit ()
to cancel the program for a serious problem (such as "the driver was not started" or "the file was not found" ). Calling exit ()
does not allow a multithreaded application, because this formula does not shut down correctly; Besides, I do not want to ever exit the main application because it is a server application , And in many cases, can do active things to recover from the main program error.
I want to change the essentially provided system exit (int status)
with my function, i.e.
class exit_exception : Public runtime_error {public: exit_exception (int position): runtime_error ("Exit from position" + to_string (position)) {}}; Extern "C" Exit zero (int position) {exit_exception (position); }
And grab the exception in my code. It seems to work, but it is clearly not a hack and the way the nature's purpose is to use exit
edit
Many people have suggested that I will keep it in a separate process, but this will harm many things. Third-party libraries have a lot of speed data transfers that need to be in the main application process because it resides in the same virtual memory space, and to assign memory to the FPGA copocessor, the malloc
Controller does not use This code is close to "iron" and every bit of memory is spreading bandwidth and spreading to PCIE boards.
Edit 2
My program can still return status code from int main ()
to the OS with return value, which will be Do not finally call exit ()
. Otherwise I will be in real trouble.
This is just an idea, but you can use a similar approach as I did When using a different version for wrapping me memcpy
, take a look at my answer.
Then you can create a replacement for exit ().
function that does nothing, or some cleaning it is just an idea and I do But it can help you solve your problem.
Comments
Post a Comment