Better replacement for exit(), atexit() in C

拜拜、爱过 提交于 2019-12-04 04:03:41

what is the best possible way of process terminating

  1. If going single threaded just use exit(), as your code is not going multi-threaded.
  2. Else make sure all but one thread have ended before the last thread and then safely call exit() because of 1. above.

Given that power/hardware fails can happen at any time, the imposs.. extreme difficulty of reliably terminating threads with user code and the chaotic nature of the use of memory pools etc. in many non-trivial multithreaded apps, it is better to design apps and systems that can clean temp files etc. on start-up, rather than trying to micro-manage shutdown.

'Clean up all the resources you allocate before you exit' sounds like good advice in a classroom or lecture, but quickly becomes a whole chain of albatross round your neck when faced with a dozen threads, queues and pools in a continually changing dynamic system.

If you can, if you are running under a non trivial OS, let it do its job and clean up for you. It's much better at it than your user code will ever be.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!