如何通过引发异常覆盖的exit(),也许 [英] How to override exit(), perhaps by throwing exception

查看:127
本文介绍了如何通过引发异常覆盖的exit(),也许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个这是没有考虑多线程和异常处理编写的第三方库。我们主要的可执行文件是多线程的,并使用了异常。

We have a third-party library that was written without multithreading or exception handling in mind. Our main executable is multithreaded and uses exceptions.

第三方库使用退出()中止严重问题的程序(如驾驶员未初始化或未找到文件)。调用在多线程应用程序退出()是不允许的,因为它没有正确关闭线程。此外,我真的不想永远退出主应用程序,因为它是一个服务器应用程序,并且在许多情况下,也有积极的东西,主程序可以做从错误中恢复。

The third-party library uses exit() to abort the program for serious problems (like "driver not initialized" or "file not found"). Calling exit() in a multithreaded application is not allowed, as it does not shut down threads correctly. In addition, I really don't want to ever exit the main application, as it is a server application, and in many cases, there are proactive things that the main program can do to recover from the error.

我想从根本上替代提供退出(INT状态)函数用我自己的功能系统,即

I would like to essentially replace the system provided exit(int status) function with my own function, ie

class exit_exception : public runtime_error 
{
    public: exit_exception(int status) 
      : runtime_error("exit called with status " + to_string(status)) {}      
};

extern "C" void exit(int status) {
    throw  exit_exception(status);
}

和赶上我的code除外。它似乎工作,但这显然是一个黑客,而不是自然意退出()来的使用方式。我在做什么不知道错了?

and catch the exception in my code. It seems to work, but this is obviously a hack and not the way nature intended exit() to be used. What am I doing wrong without knowing?

很多人建议我把这个在一个单独的进程,但是这会破坏很多东西。第三方库确实非常高速的数据传输,需要在主应用程序的过程,因为它生活在相同的虚拟内存空间,并且不使用的malloc 来分配内存从FPGA协处理器是控制器。这code是接近铁,并挤压带宽的每一位出内存和PCIe总线。

Many have suggested I put this in a separate process, but that would defeat many things. The third-party library does very high speed data transfer that needs to be in the main application process because it lives in the same virtual memory space and does not use malloc to allocate memory from the FPGA coprocessor that is controller. This code is close to the "iron" and is squeezing every bit of bandwidth out of the memory and PCIe busses.

我的程序仍然可以从返回状态codeS与返回值的OS INT主要(),这确实的的最终叫退出()。否则,我将是真正的麻烦。

My program can still return status codes to the OS with the return value from int main(), which does not ultimately call exit(). Otherwise I would be in real trouble.

推荐答案

最大和最明显的风险是资源泄漏。
如果库认为它的错误处理策略是潜水离您最近的窗口有送花儿给人一种风险,即抛该异常不会导致内存和系统资源的组织良好的释放。

The biggest and most obvious risk is resource leakage. If the library thinks its error handling strategy is to dive out of the nearest window there's alway a risk that throwing that exception isn't going to result in well organised release of memory and system resources.

不过,我注意到你提到它不会与分配内存的malloc()如果这意味着你必须为它提供所有资源作为缓冲液等,那么也许一些神奇的意外是安全unwindable!

However I notice you mention it doesn't allocate memory with malloc() if that means you have to provide it with all its resources as buffers and the like then maybe by some miraculous accident it is safely unwindable!

如果失败了所有我可以建议是与供应商联系,并说服他们,他们应该加入我们的行列其余21编程范例。

If that fails all I can suggest is contact the supplier and persuade them that they should join the rest of us in 21st programming paradigms.

PS:抛出异常出一个的atexit的()处理程序引起的C ++程序终止。所以加入一个投掷的处理程序是不是一种选择。
这也是不是一种选择,如果一些其他图书馆后你插入一个'的atexit()处理程序。

PS: Throwing an exception out of an atexit() handler causes termination of C++ programs. So adding a throwing handler is not an option. It's also not an option if some other library inserts an 'atexit()' handler after yours.

这篇关于如何通过引发异常覆盖的exit(),也许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆