Custom Search

Wednesday, January 23, 2008

C++ - atexit implementation

More than one occasion - when a program exits - we may want to do some cleanup operations and notify other processes ( this becomes extremely important in the case of child-parent processes).

C/C++ does have a function called atexit() that does that.

A sample code fragment to use the same is as follows.


#include <cstdlib>
#include <iostream>

using namespace std;

void do1()
{
std::cout << "do1 implementation\n";
}

void do2()
{
std::cout << "do2 implementation\n";
}


int main()
{
atexit(do1);
atexit(do2);

exit(EXIT_SUCCESS);
}

Sample output:


$ g++ -Wall -Werror testexit.cpp
[noname@localhost test]$ ./a.out
do2 implementation
do1 implementation

No comments: