Open In App

Difference between std::quick_exit and std::abort

Last Updated : 15 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

 

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.

Let us see the differences in a tabular form -:

  std::quick_exit std::abort
1. It is defined in header file <cstdlib> It is defined in header file <cstdlib>
2.

Its syntax is -:

quick_exit( int exit_code )

Its syntax is -:

abort();

3. It only takes one parameters It does not takes any parameters.
4. Its return value is void. Its return value is void.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads