Difference between std::quick_exit and std::abort

std::quick_exit()

It Causes normal program termination to occur without completely cleaning the resources.
Syntax :

 void quick_exit(int exit_code) no except; 

In case of execution of threads the codes becomes complex and knowing the execution of threads is hard. While one the thread might be waiting for a process to end while the other thread is waiting for the previous thread. In such cases real programs usually deadlock and exit which isn’t pleasant. The admin must force start the system or close the user interface etc to come out of the lock. For this purposes the std::quick_exit() was made. It terminates the program saving us from the hard part of coming out of a deadlock while executing a real program. It clears the IO but does not flush the static destructors. Another function at_quick_exit()

 int at_quick_exit( void (*func)(void) ); 

takes a functions as parameter which is to be executed when the quick_exit() function is called. The functions registered to this are called on the reverse order of their execution.

#include <cstdlib>
void atEXIT()
{
  cout << "Quick exit function.";
}
int main ()
{
  at_quick_exit(atEXIT);
  cout << "Main Function";
  quick_exit(0);
  cout << "End of Main"; 
  return 0;
}
 Output:  Main Function
          Quick exit function.    

std::abort()

Causes abnormal program termination unless SIGABRT is being caught by a signal handler passed to std::signal and the handler does not return.
Syntax:

 void abort() no except; 

POSIX specifies that the abort function overrides blocking or ignoring the SIGABRT signal.

#include <cstdlib>

int main ()
{
  FILE * fp;
  fp= fopen("myfile.txt", "r");
  if (fp== NULL)
  {
    fputs("Error opening file \n", stderr);
    abort();
  }
  fclose(fp);
  return 0;
}
 Output:  The file named myfile.txt, if not found or fails opens due to any reason the error 
            message is printed and abort function terminates the program.

Similarities : Both the std::quick_exit() and std::abort() functions are defined in the cstdlib header. Both of these functions have no parameters and no exceptions. They both have no return value as well. They are used for termination of the program but they still are different from each other.
Differences : The reason for introducing the quick_exit() function was to end the running program while clearing the IO and still getting some part of the program i.e. the functions registered in at_quick_exit() to be executed whereas the std::abort() function terminates the program without executing furthermore part of the program and not clearing the IO.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AkshatBhardwaj