What is the best way to destroy a singleton object?
case A: Single threaded Environment
case B: Multi Threaded Environment
Sample snippets(if any) will be really helpful.
[EDIT] I don't have a specific use case I am just trying to understand that IF AT ALL the singleton has to be used how to destroy it correctly. As i understand, from the comments, there are 2 scenarios possible:
1. Destroy the singleton when no code is accessing it.(use smart pointers which will take care of destroying the object by itself using RAII)
2. Destroy a singleton when exiting the program irrespective of whether or not some code was holding on to the singleton.
(explicitly destroy by deleting the instance just before main exits)
The backlash against the last decade's overuse of Singletons seem to be in rude health, but they're not entirely evil or unjustified... programming is about compromises and being practical, and it's hard to generalise (generally ;-P). By all means revisit the design and see if you can usefully get rid of them, but if not - so be it.
Anyway, if you want to understand the trade offs, you can't do better than start by reading Alexandrescu's Modern C++ Design, which devotes a chapter to the alternatives for Singletons. Basically, you're asking a silly question here because we don't know what operational constraints your singleton(s) have... what potential interactions, which resources they may need to use and whether they can be re-opened after being closed etc.. So, spit it out or settle for silly answers ;-P.
Leaving aside the question of if it is a good idea.
Which we should do in a separate question!
The multi-thread initialization of the object is the only real problem. You can handle this two ways.
Example Guard
In multi-threaded,
In single-threaded:
If you're going to use a global, I prefer something like this:
This gives you:
What you still have to worry about:
Might be able to use atexit() if you only care about cleaning up on successful shutdowns.
Don't create it in the first place!
Seriously, I strongly recommend you reconsider your choice of singleton, especially in a multithreaded environment. Instead just create an instance in
main()
and pass it down the call hierarchy to where it is needed.You can use something like
shared_ptr
to ensure that the object stays around until no-one needs it any more.