C++ cstdlib at_quick_exit


The cstdlib at_quick_exit function register a function which is to be called when the ‘quick_exit’ is called.

extern “C” int at_quick_exit(void (*f)(void)) noexcept;
extern “C++” int at_quick_exit(void (*f)(void)) noexcept;

Parameters:
f* -A pointer to function that is to be called at ‘quick_exit’ call and that accept no argument.

Return type
int -Zero if the registration succeeds,non-zero if it fails.

The ‘at_quick_exit‘ behave like the ‘at_exit‘ function which calls the function which it registered when the program terminates.However,you must note that ‘at_quick_exit’ registration is different from ‘at_exit’ registration.

Link :C++ cstdlib atexit

Code example

void func( )
{
cout<< “func() called \n” ;
}

void func1(int) { }

int main( )
{
at_quick_exit( func );

//at_quick_exit( func1 ); //error because ‘func1’ accept int argument

quick_exit( 0 );

cin.get( );
return 0;
}

Output

func() called

Link :C++ cstdlib quick_exit


 


Stop wasting time,earn money($$$) from your website-Join Now!

Some points to note about ‘at_quick_exit’ call:

i) It is unspecified whether the call to ‘at_quick_exit’ will succeed if it is call after ‘quick_exit’.(In Visual Studio the call fails).

Code example:Tested in Visual Studio

void func( ) { cout<<“func() called\n”; }

int main( )
{
quick_exit( 0 );

at_quick_exit( func );

cin.get( );
return 0;
}

Output is empty,which means ‘at_quick_exit’ is not called;in VS type ‘Ctrl+F’ to run without debugging.


ii) The order of registration performed by ‘at_quick_exit’ is indeterminate when it is called from different thread.The meaning of this statement is exactly what it is stated.While running multithread program multiple functions registered by calling ‘at_exit_call’ have no specific order in which the (registered)functions is call at ‘quick_exit’ call.

Code example:The code below is tested in VS,and include the header <thread>

void func( )
{
cout << “func() called \n” ;
}

void func1()
{
cout<< “func1() called \n”;
}

void foo( )
{
at_quick_exit( func );
}

void poo( )
{
at_quick_exit( func1 );
}

int main( )
{
thread helper( foo );

thread helper1( poo )

cout<< “Callig helper1.join( ) \n” ;
helper1.join( ) ;

cout<< “calling helper2.join( ) \n” ;
helper2.join( ) ;

quick_exit( 0 ) ;

cin.get( );
return 0;
}

Output

Callig helper1.join( )
calling helper2.join( )
func() called
func1() called

It seems ‘func()’ is call first ,followed by ‘func1()’.Check the output in your compiler;it might differ.


iii) The function does not introduce data race.

iv) The at_quick_exit can register at least 32 functions.You may call ‘at_quick_exit’ with different argument-pointer to function- for at least 32 times.

v) If ‘abort()’ is call the function registered by ‘at_quick_exit’ is not called.

Code example

void func( ) { cout<< “func() called \n”; }

int main( )
{
cout<<“Calling ‘at_quick_exit’ \n”;
at_quick_exit( func );

cout<<“Calling ‘abort’ \n”;
abort( );

quick_exit( 0 );

cin.get( );
return 0;
}

Output

Calling ‘at_quick_exit’
Calling ‘abort’

The program is directly terminated at abort() call and so ‘func()’ is not called.Note ‘abort()’ is dangerous and it’s must be used judiciously.

Link :C++ cstdlib abort





Using class member function to register

The class member function cannot be used for registration using ‘at_quick_exit’ function.The ‘at_quick_exit’ function accept an argument of ‘void (*)(void)’ type.But if you try to pass a pointer to any class member function you will passing an entity of type (say the return type and the argument type is void) ‘void (CLASS_NAME::*)(void)’ which is not compatible with the required type:void (*)(void).The compiler will complain in this case.

Code example

class A
{
public:
A() = default;

void func( ) { };

~A() =default;
};

int main( )
{
at_quick_exit( &A::func ) ; //error!

quick_exit( 0 );

cin.get( );
return 0;
}

To make the member function registrable you can either make it as a friend function or make it a static type.Consider the code below.

Code example

class A
{
public:
A() = default;

friend void func( ) { cout<<“func() called \n”; };

static void sfunc( ) { cout<<“sfunc() called \n”; };

~A() =default;
};

int main( )
{
at_quick_exit( func ) ; //work fine

at_quick_exit( A::sfunc ) ; //work fine

quick_exit( 0 );

cin.get( );
return 0;
}

Output

sfunc() called
func() called

The program executes smoothly without any error.