std::atexit(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::atexit πŸ’–

okay bestie, welcome to the C++ Standard Libary manual page for std::atexit (page std::atexit(3)) β€” yes, the β€œLibary” spelling is right there in the header and we are not fixing it, we are respecting it πŸ’…. This is the function that registers a function to run on normal program termination, and the vibe is very β€œcleanup crew but make it C++” ✨

🌸 NAME 🌸

std::atexit β€” std::atexit.

That’s it. That’s the name section. Minimal, iconic, periodt.

✨ Synopsis ✨

Header tea first:

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

Okay so what does it do? It registers the function pointed to by func to be called on normal program termination β€” like via std::exit() or returning from the main function 🫢.

Here’s the ordering tea:

  • 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)
  • (since 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.

Also: the same function may be registered more than once. No cap, you can double-book that handler πŸ’–.

If a function exits via an exception, std::terminate is called. So bestie, do not let your exit handler yeet an exception out into the void 😭.

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

The implementation is guaranteed to support the registration of at least 32 functions. The exact limit is implementation-defined. So the minimum is 32, and your implementation’s vibe check decides the rest πŸ’….

πŸŽ€ Parameters πŸŽ€

  • func β€” pointer to a function to be called on normal program termination.

🌈 Return value 🌈

0 if the registration succeeds, nonzero value otherwise. Zero is the success slay; nonzero is the β€œregistration said no” energy.

πŸ’œ Notes πŸ’œ

The two overloads are distinct because the types of the parameter func are distinct β€” language linkage is part of its type. Yes, this is one of those C++ details that is lowkey doing the most, and it is valid, fr fr.

πŸ¦‹ Example πŸ¦‹

// Run this code

#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

See that reverse order? Registration order was handler 1 then handler 2, and the calls came back handler 2 then handler 1. The reverse-order rule ate and left no crumbs πŸ’….

πŸ”₯ See also πŸ”₯

  • abort β€” causes abnormal program termination (without cleaning up) (function)
  • exit β€” causes normal program termination with cleaning up (function)
  • quick_exit β€” causes quick program termination without completely cleaning up (C++11) (function)
  • at_quick_exit β€” registers a function to be called on std::quick_exit invocation (C++11) (function)
  • C documentation for atexit

πŸ’Œ source/version: http://cppreference.com, date: 2024.06.10, page: std::atexit(3) β€” bye bestie, go register those exit handlers and exit cleanly πŸ’–πŸ«Ά

πŸ“¦ where this page comes from