C ++异常和信号处理程序 [英] C++ exceptions and signal handlers

查看:172
本文介绍了C ++异常和信号处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Bjarne Stroustrup的 C ++的设计和进化。关于异常处理和异步信号,提到如下:

I am reading The Design and Evolution of C++, by Bjarne Stroustrup. Regarding exeception handling and asynchronous signals it is mentioned as below:


异常可以用于处理信号等事情吗?几乎肯定不是在大多数C环境中。麻烦的是,C使用的函数像malloc不是re-entrant。如果中断发生在malloc中间并引发异常,则无法阻止异常处理程序再次执行malloc。

Can exceptions be used to handle things like signals? Almost certainly not in most C environments. The trouble is that C uses functions like malloc that are not re-entrant. If an interrupt occurs in the middle of malloc and causes an exception, there is no way to prevent the exception handler from executing malloc again.

一个C ++实现,其中调用序列和整个运行时库是围绕重入的要求设计的,这将使信号可能抛出异常

A C++ implementation where calling sequences and the entire run-time library are designed around the requirement for re-entrancy would make it possible for signals to throw exceptoins

作者的意思是没有办法阻止异常处理程序再次执行malloc?

What does the author mean by the statement "there is no way to prevent the exception handler from executing malloc again"? How would making functions re-entrant make it possible to throw exceptions from signal handlers?

推荐答案

在我看来,部分不是信号处理程序

In my opinion that part doesn't really make much sense with current C++.

在C ++中没有办法使用异常作为一个信号,因为信号意味着执行一个处理程序,然后(可能)继续执行。

In C++ there is no way to use an exception as a signal because signals are meant to be executing an handler and then (possibly) continue execution.

C ++异常不会以这种方式工作。一旦你到达异常处理程序堆栈已经回滚,并且没有办法在处理后继续:没有办法获得到语句后抛出(或下面的函数调用期间异常获取

C++ exceptions however don't work this way. Once you get to the exception handler the stack has been already rolled back and there is no way to "continue" after the handling: there is no way to get to the statement following a throw (or following a function call during which an exception gets thrown).

信号是异步的,但不是破坏性的,并且在信号可能之后继续(即使当然必须注意信号处理程序中做了什么)

Signals are asynchronous but not disruptive and continuing after a signal is possible (even if of course care must be taken about what is done in the signal handler), exceptions instead are disruptive of the program flow and continuing is impossible.

我会说这两个想法在逻辑层次是不兼容的,如果图书馆

I'd say the two ideas are incompatible at a logical level and doesn't really matter if the library is reentrant or not.

可能是在早期的C ++设计中有一个例外的恢复选项...

May be in early C++ design there was a resume option for exceptions...

这篇关于C ++异常和信号处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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