Namespaces
Variants

std:: at_quick_exit

From cppreference.net
Utilities library
定义于头文件 <cstdlib>
int at_quick_exit ( /*atexit-handler*/ * func ) noexcept ;
int at_quick_exit ( /*c-atexit-handler*/ * func ) noexcept ;
(1) (C++11 起)
extern "C++" using /*atexit-handler*/ = void ( ) ;
extern "C" using /*c-atexit-handler*/ = void ( ) ;
(2) ( 仅用于说明* )

注册由 func 指向的函数,使其在程序快速终止时(通过 std::quick_exit )被调用。

从多个线程调用该函数不会引发数据竞争。该实现保证至少支持32个函数的注册。具体限制由实现定义。

注册的函数不会在 程序正常终止 时被调用。若需在此情况下调用函数,必须使用 std::atexit

目录

参数

func - 指向程序快速终止时被调用函数的指针

返回值

0 表示注册成功,非零值表示注册失败。

注释

这两个重载之所以不同,是因为参数 func 的类型不同( 语言链接 是其类型的一部分)。

示例

#include <cstdlib>
#include <iostream>
void f1()
{
    std::cout << "pushed first" << std::endl; // flush is intentional
}
extern "C" void f2()
{
    std::cout << "pushed second\n";
}
int main()
{
    auto f3 = []
    {
        std::cout << "pushed third\n";
    };
    std::at_quick_exit(f1);
    std::at_quick_exit(f2);
    std::at_quick_exit(f3);
    std::quick_exit(0);
}

输出:

pushed third
pushed second
pushed first

参见

导致程序异常终止(不进行清理操作)
(函数)
导致程序正常终止并进行清理操作
(函数)
注册在调用 std::exit() 时要执行的函数
(函数)
(C++11)
导致程序快速终止但不完全清理
(函数)
C 文档 关于 at_quick_exit