gpt4 book ai didi

c++ - 信号处理程序中不允许使用对象或函数

转载 作者:太空宇宙 更新时间:2023-11-04 13:48:28 30 4
gpt4 key购买 nike

我正在寻找“信号处理程序中不允许的对象或函数” 的定义。它在秒中使用。 N3797 的 3.6.3/4:

If there is a use of a standard library object or function not permitted within signal handlers (18.10) that does not happen before (1.10) completion of destruction of objects with static storage duration and execution of std::atexit registered functions (18.5), the program has undefined behavior.

一般来说,我想为这个 UB 规则找到一个例子。

最佳答案

在 18.10/9 引用的草案中:

The common subset of the C and C++ languages consists of all declarations, definitions, and expressions that may appear in a well formed C++ program and also in a conforming C program. A POF (“plain old function”) is a function that uses only features from this common subset, and that does not directly or indirectly use any function that is not a POF, except that it may use functions defined in Clause 29 that are not member functions. All signal handlers shall have C linkage. A POF that could be used as a signal handler in a conforming C program does not produce undefined behavior when used as a signal handler in a C++ program. The behavior of any other function used as a signal handler in a C++ program is implementation-defined.228

信号处理程序中允许使用在 C++ 和 C 中都有效的函数。此外,您可以使用原子非成员函数(第 29 条)。实现可能允许其他功能工作,但请注意:

228) In particular, a signal handler using exception handling is very likely to have problems. Also, invoking std::exit may cause destruction of objects, including those of the standard library implementation, which, in general, yields undefined behavior in a signal handler (see 1.9).

根据您引用的规则,在大多数情况下,在main 完成并且所有静态变量都被销毁后让一些线程继续运行会导致UB。仅当函数是信号安全的且不使用标准库时才允许。

关于c++ - 信号处理程序中不允许使用对象或函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24631444/

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