gpt4 book ai didi

c++ - 如何编写处理信号的跨平台 C++?

转载 作者:可可西里 更新时间:2023-11-01 09:21:20 26 4
gpt4 key购买 nike

这个问题更多的是出于我个人的好奇心,而不是任何重要的事情。我试图让我的所有代码至少与 Windows 和 Mac 兼容。到目前为止,我已经了解到我应该将我的代码基于 POSIX,这很好但是......

Windows 没有sigaction 函数所以使用了signal?根据: What is the difference between sigaction and signal?信号有问题。

  1. The signal() function does not block other signals from arriving while the current handler is executing; sigaction() can block other signals until the current handler returns.
  2. The signal() function resets the signal action back to SIG_DFL (default) for almost all signals. This means that the signal() handler must reinstall itself as its first action. It also opens up a window of vulnerability between the time when the signal is detected and the handler is reinstalled during which if a second instance of the signal arrives, the default behaviour (usually terminate, sometimes with prejudice - aka core dump) occurs.

如果两个 SIGINT 很快到达,则应用程序将以默认行为终止。有什么办法可以解决此问题?这两个问题对一个进程有什么其他影响,例如想要阻止 SIGINT?在使用信号时我可能会遇到其他问题吗?我该如何修复它们?

最佳答案

您真的根本不想处理 signal()。

你想要“事件”。

理想情况下,您会找到一个可移植到您希望定位的所有主要环境的框架 - 这将决定您对“事件”实现的选择。

这里有一个有趣的话题可能会有所帮助:

Game Objects Talking To Each Other

附言:signal() 和 sigaction() 之间的主要区别是 sigaction() 是类固醇上的“signal()”——更多选项,允许 SA_RESTART 等。我不鼓励使用任何一个,除非你真的、真的需要。

关于c++ - 如何编写处理信号的跨平台 C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9092478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com