简体   繁体   English

使用atexit功能的场景是什么?

[英]What's the scenario to use atexit function?

CRT function atexit() could register a function to run after main function returns. CRT函数atexit()可以在main函数返回后注册一个函数来运行。 I am wondering what's the typical scenario to use this? 我想知道使用它的典型场景是什么? Is it ( atexit ) really necessary? 是( atexit )真的有必要吗?

I guess its primary use is when you don't have control over main and you want to make sure something is called at the end of it. 我猜它的主要用途是当你无法控制main ,你想确保在它结束时调用某些东西。

It's sometimes used by libraries that don't want to insist that the user program calls their cleanup functions explicitly before terminating the program. 它有时被库使用,它们不希望在终止程序之前,用户程序明确地调用它们的清理函数。

It's also used in the phoenix singleton pattern (see Modern C++ Design by Andrei Alexandrescu). 它也用于凤凰单身模式(参见Andrei Alexandrescu的Modern C ++ Design)。

It can be used for what you want that need to be executed EVERYTIME an application is shutting down. 它可以用于您需要执行的任务,每当应用程序关闭时。 By using that, you don't need to bloat your code by adding all the cleanup code before each exit() you can find in your code. 通过使用它,您不需要在代码中找到的每个exit()之前添加所有清理代码来膨胀您的代码。

Some use cases : 一些用例:

  • Clean a temporary folder 清理临时文件夹
  • Print a memory dump 打印内存转储

One of the main uses of atexit is for libraries to perform cleanup on program exit. atexit的主要用途之一是库在程序退出时执行清理。 Note that atexit is called when exit is called, not when the program aborts or crashes, so you can't do cleanup on assertion failures and so on. 请注意,调用exit会调用atexit而不是在程序中止或崩溃时调用,因此您无法对断言失败等进行清理。 It is also not called if the program calls exec . 如果程序调用exec也不会调用它。

You can call it directly in the main program if you want, if you have a library that might call exit for some reason. 如果你想要的话,你可以直接在主程序中调用它,如果你有一个可能因某种原因调用exit的库。

Note that you can only register a limited number of atexit handlers, where 'limited' depends on your operating system, and so it returns an error status. 请注意,您只能注册有限数量的atexit处理程序,其中“limited”取决于您的操作系统,因此它会返回错误状态。

It gives C programs a similar ability to calling the destructor of a static variable in C++. 它为C程序提供了类似的能力,可以在C ++中调用静态变量的析构函数。

I've used it to delete temporary files, or (once or twice) to reset some hardware registers. 我用它来删除临时文件,或者(一次或两次)重置一些硬件寄存器。 Generally it's not necessary to use it to close files or rlease memory, because the O/S will do that for you. 通常没有必要使用它来关闭文件或释放内存,因为操作系统会为你做这件事。

When writing libraries ... Imagine a library that on crashes save the stack on a pre-defined path (or mails the trace). 在编写库时...想象一下在崩溃时将堆栈保存在预定义路径上(或邮件跟踪)的库。


EDIT - as mentioned on the comment, this answer is wrong. 编辑 - 正如评论中提到的,这个答案是错误的。 Don't read it. 不读它。 Too late. 太晚了。

Exceptions can be handled in atexit ().Assume multi process environment. 可以在atexit()中处理异常.Assume多进程环境。 There is one hardware resource is physically available. 有一个物理可用的硬件资源。 Any one of the process can use that h/w at a time. 任何一个进程都可以一次使用该h / w。 Now process1 is acquired the h/w resource and after processing process1 not released h/w resource. 现在process1获取了h / w资源,并且在处理process1之后没有释放h / w资源。 To release the h/w resource this atexit() may be used, so that process2 can acquire h/w effectively. 为了释放h / w资源,可以使用此atexit(),以便process2可以有效地获取h / w。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM