Re: Singleton_pattern and Thread Safety
On 12/11/10 02:16 AM, Leigh Johnston wrote:
On 10/12/2010 09:52, James Kanze wrote:
Note that the above still risks order of destruction issues;
it's more common to not destruct the singleton ever, with
something like:
namespace {
Singleton* ourInstance =&Singleton::instance();
Singleton&
Singleton::instance()
{
if (ourInstance == NULL)
ourInstance = new Singleton;
return *ourInstance;
}
}
(This solves both problems at once: initializing the variable
with a call to Singleton::instance and ensuring that the
singleton is never destructed.)
James "Cowboy" Kanze's OO designs includes objects that are never
destructed but leak instead? Interesting. What utter laziness typical of
somebody who probably overuses (abuses) the singleton pattern. Singleton
can be considered harmful (use rarely not routinely).
What James describes is a very common idiom (to avoid order of
destruction issues) and it's certainly one I've often used.
--
Ian Collins