std::at_quick_exit(3)

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

✨ std::at_quick_exit ✨

Welcome to the C++ Standard Libary manual page for std::at_quick_exit, bestie. It’s giving β€œregister a function for quick program termination,” and honestly? Iconic. 🫢

πŸŽ€ NAME πŸŽ€

std::at_quick_exit β€” yes, the name is literally the function name too. The page is called std::at_quick_exit, and it’s all about registering stuff to run when the program dips out via a quick exit. πŸ’…

🌈 Synopsis 🌈

Defined in header <cstdlib>.

int at_quick_exit( /*atexit-handler*/* func ) noexcept;   (1) (since C++11)
int at_quick_exit( /*c-atexit-handler*/* func ) noexcept;
extern "C++" using /*atexit-handler*/ = void();           (2)
(exposition only*)
extern "C" using /*c-atexit-handler*/ = void();

Okay so here’s the tea: this registers the function pointed to by func to be called on quick program termination (via std::quick_exit). πŸ¦‹

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. ✨

🚨 The registered functions will not be called on normal program termination. If a function need to be called in that case, std::atexit must be used. No cap.

πŸ¦‹ Parameters πŸ¦‹

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

πŸ’œ Return value πŸ’œ

0 if the registration succeeds, nonzero value otherwise. She’s simple, she’s clear, she’s giving successful-registration energy. πŸŽ€

🌸 Notes 🌸

The two overloads are distinct because the types of the parameter func are distinct β€” language linkage is part of its type. Lowkey a little language-lawyer moment, but we respect it. πŸ’…

πŸ”₯ Example πŸ”₯

Run this code πŸ‘‡

// Run this code

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

Output:

pushed third
pushed second
pushed first

πŸ‘‘ See also πŸ‘‘

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

Source/version/date vibe check at the very end: http://cppreference.com Β· 2024.06.10 Β· std::at_quick_exit(3). Bye bestie β€” go register those quick-exit functions responsibly πŸ’–βœ¨

πŸ“¦ where this page comes from