首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::atexit

Defined in header <cstdlib>

int atexit( /*c-atexit-handler*/* func ); int atexit( /*atexit-handler*/* func );

(1)

extern "C++" using /*atexit-handler*/ = void(); // exposition-only extern "C" using /*c-atexit-handler*/ = void(); // exposition-only

(2)

注册所指向的函数。func在正常程序终止%28via时被调用std::exit()或从主要功能29%。

The functions will be called during the destruction of the static objects, in reverse order: if A was registered before B, then the call to B is made before the call to A. Same applies to the ordering between static object constructors and the calls to atexit: see std::exit

(until C++11)

The functions may be called concurrently with the destruction of the objects with static storage duration and with each other, maintaining the guarantee that if registration of A was sequenced-before the registration of B, then the call to B is sequenced-before the call to A, same applies to the sequencing between static object constructors and calls to atexit: see std::exit

(since C++11)

同一功能可以多次注册。

如果函数通过异常退出,std::terminate叫做。

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

保证执行至少支持注册32职能。确切的限制是实现定义的。

参数

func

-

pointer to a function to be called on normal program termination

返回值

​0​如果注册成功,则为非零值。

注记

这两个重载是不同的,因为参数的类型。func分别为%28语言链接是其类型%29的一部分。

例外

(none)

(until C++11)

noexcept specification: noexcept

(since C++11)

二次

代码语言:javascript
复制
#include <iostream>
#include <cstdlib>
 
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 != 0) or (result_2 != 0)) {
        std::cerr << "Registration failed\n";
        return EXIT_FAILURE;
    }
 
    std::cout << "returning from main\n";
    return EXIT_SUCCESS;
}

二次

产出:

二次

代码语言:javascript
复制
returning from main
at exit #2
at exit #1

二次

另见

at_quick_exit (C++11)

registers a function to be called on quick_exit invocation (function)

c出口文件

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券