Namespaces
Variants

std:: atexit

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

注册由 func 指向的函数,使其在程序正常终止时被调用(通过 std::exit() 或从 main 函数 返回)

这些函数将在静态对象析构期间被调用,调用顺序与注册顺序相反:若 A 先于 B 注册,则 B 的调用先于 A 的调用。静态对象构造函数与 atexit 调用之间的顺序规则同样适用:参见 std::exit

(C++11 前)

这些函数可能与具有静态存储期的对象析构及其他 atexit 调用并发执行,同时保证:若 A 的注册顺序先于 B 的注册顺序,则 B 的调用顺序先于 A 的调用顺序。静态对象构造函数与 atexit 调用之间的顺序规则同样适用:参见 std::exit

(C++11 起)

同一函数可能被多次注册。

如果函数通过异常退出,将调用 std::terminate

atexit 是线程安全的:从多个线程调用该函数不会引发数据竞争。

实现保证至少支持注册32个函数。具体上限由实现定义。

目录

参数

func - 指向正常程序终止时要调用的函数的指针

返回值

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

注释

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

示例

#include <cstdlib>
#include <iostream>
void atexit_handler_1()
{
    std::cout << "At exit #1\n";
}
void atexit_handler_2()
{
    std::cout << "At exit #2\n";
}
int main()
{
    const int result_1 = std::atexit(atexit_handler_1);
    const int result_2 = std::atexit(atexit_handler_2);
    if (result_1 || result_2)
    {
        std::cerr << "Registration failed!\n";
        return EXIT_FAILURE;
    }
    std::cout << "Returning from main...\n";
    return EXIT_SUCCESS;
}

输出:

Returning from main...
At exit #2
At exit #1

参见

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