Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::atexit(3)		      C++ Standard Libary		std::atexit(3)

NAME
       std::atexit - std::atexit

Synopsis
	  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;
       (since C++11)
	  int atexit( /*atexit-handler*/* func ) noexcept;		 (1)
	  extern "C++" using /*atexit-handler*/	= void(); //
	  exposition-only
       (2)
	  extern "C" using /*c-atexit-handler*/	= void(); //
	  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
       (until C++11)
	  between static object	constructors and the calls to atexit: see
	  std::exit
	  The functions	may be called concurrently with	the destruction	of the
	  objects with static storage duration and with	each other,  maintain-
       ing
	  the  guarantee  that	if  registration of A was sequenced-before the
       (since C++11)
	  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

	  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.

Parameters
	  func - pointer to a function to be called on normal program termina-
       tion

Return value
	  0 if the registration	succeeds, nonzero value	otherwise.

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

Example
       // Run this code

	#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)	|| (result_2 !=	0)) {
		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
	  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)

http://cppreference.com		  2022.07.31			std::atexit(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::atexit&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help