Throwing an exception from within a signal handler

后端 未结 7 1972
半阙折子戏
半阙折子戏 2020-11-30 03:56

We have a library that deals with many aspects of error reporting. I have been tasked to port this library to Linux. When running though my little test suite, one of the

7条回答
  •  隐瞒了意图╮
    2020-11-30 04:50

    Signals are totally different than C++ exceptions. You can't use a C++ try/catch block to handle a signal. Specifically, signals are a POSIX concept, not a C++ language concept. Signals are delivered asynchronously to your application by the kernel, whereas C++ exceptions are synchronous events defined by the C++ standard.

    You are quite limited in what you can do portably in a POSIX signal handler. A common strategy is to have a global flag of type sig_atomic_t which will be set to 1 in the signal handler, and then possibly longjmp to the appropriate execution path.

    See here for help writing proper signal handlers.

提交回复
热议问题