Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/program/atexit"

From cppreference.com
< cpp‎ | utility‎ | program
m (Update links.)
m (., fmt)
 
(22 intermediate revisions by 12 users not shown)
Line 1: Line 1:
{{cpp/title| atexit}}
+
{{cpp/title|atexit}}
 
{{cpp/utility/program/navbar}}
 
{{cpp/utility/program/navbar}}
{{ddcl | header=cstdlib |
+
{{dcl begin}}
extern "C"  int atexit( void (*func)() );
+
{{dcl header|cstdlib}}
extern "C++" int atexit( void (*func)() );
+
{{dcl rev multi|num=1|until1=c++11|dcl1=
 +
int atexit( /* c-atexit-handler */* func );
 +
int atexit( /* atexit-handler */* func );
 +
|dcl2=
 +
int atexit( /* c-atexit-handler */* func ) noexcept;
 +
int atexit( /* atexit-handler */* func ) noexcept;
 
}}
 
}}
 +
{{dcl|num=2|notes={{mark expos}}|1=
 +
extern "C" using /* c-atexit-handler */ = void();
 +
extern "C++" using /* atexit-handler */ = void();
 +
}}
 +
{{dcl end}}
  
Registers the function pointed to by {{tt|func}} to be called on normal program termination (via {{rlpf|exit}} or returning from {{tt|main()}}).
+
Registers the function pointed to by {{c|func}} to be called on normal program termination (via {{lc|std::exit()}} or returning from the [[cpp/language/main function|main function]])
 +
{{rrev multi|until1=c++11|rev1=
 +
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 {{tt|atexit}}: see {{lc|std::exit}}.|rev2=
 +
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 {{tt|atexit}}: see {{lc|std::exit}}.
 +
}}
  
Calling the function from several threads does not induce a data race. The implementation shall support the registration of at least {{c|32}} functions.
+
The same function may be registered more than once.
 +
 
 +
If a function exits via an exception, {{lc|std::terminate}} is called.
 +
 
 +
{{tt|atexit}} is thread-safe: calling the function from several threads does not induce a data race.
 +
 
 +
The implementation is guaranteed to support the registration of at least 32 functions. The exact limit is implementation-defined.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | func | pointer to a function to be called on normal program termination}}
+
{{par|func|pointer to a function to be called on normal program termination}}
 
{{par end}}
 
{{par end}}
  
Line 18: Line 38:
 
{{c|0}} if the registration succeeds, nonzero value otherwise.
 
{{c|0}} if the registration succeeds, nonzero value otherwise.
  
===Exceptions===
+
===Notes===
{{noexcept}}
+
The two overloads are distinct because the types of the parameter {{c|func}} are distinct ({{lt|cpp/language/language linkage}} is part of its type).
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <cstdlib>
| output=
+
#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;
 +
}
 +
|output=
 +
Returning from main...
 +
At exit #2
 +
At exit #1
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/utility/program/dsc at_quick_exit}}
+
{{dsc inc|cpp/utility/program/dsc abort}}
{{dsc see c | c/program/atexit}}
+
{{dsc inc|cpp/utility/program/dsc exit}}
 +
{{dsc inc|cpp/utility/program/dsc quick_exit}}
 +
{{dsc inc|cpp/utility/program/dsc at_quick_exit}}
 +
{{dsc see c|c/program/atexit}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/utility/program/atexit]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/utility/program/atexit]]
+
[[fr:cpp/utility/program/atexit]]
+
[[it:cpp/utility/program/atexit]]
+
[[ja:cpp/utility/program/atexit]]
+
[[pt:cpp/utility/program/atexit]]
+
[[ru:cpp/utility/program/atexit]]
+
[[zh:cpp/utility/program/atexit]]
+

Latest revision as of 21:54, 25 October 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <cstdlib>
(1)
int atexit( /* c-atexit-handler */* func );
int atexit( /* atexit-handler */* func );
(until C++11)
int atexit( /* c-atexit-handler */* func ) noexcept;
int atexit( /* atexit-handler */* func ) noexcept;
(since C++11)
extern "C" using /* c-atexit-handler */ = void();
extern "C++" using /* atexit-handler */ = void();
(2) (exposition only*)

Registers the function pointed to by func to be called on normal program termination (via std::exit() or returning from the main function)

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)

The same function may be registered more than once.

If a function exits via an exception, std::terminate is called.

atexit is thread-safe: calling the function from several threads does not induce a data race.

The implementation is guaranteed to support the registration of at least 32 functions. The exact limit is implementation-defined.

Contents

[edit] Parameters

func - pointer to a function to be called on normal program termination

[edit] Return value

0 if the registration succeeds, nonzero value otherwise.

[edit] Notes

The two overloads are distinct because the types of the parameter func are distinct (language linkage is part of its type).

[edit] Example

#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;
}

Output:

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

[edit] See also

causes abnormal program termination (without cleaning up)
(function) [edit]
causes normal program termination with cleaning up
(function) [edit]
causes quick program termination without completely cleaning up
(function) [edit]
registers a function to be called on std::quick_exit invocation
(function) [edit]
C documentation for atexit