Re: Singletons: can they be ultra-safe?
On 21 juin, 17:21, neelsm...@rediffmail.com wrote:
Hi,
This is one of the questions asked to me and I wanted your opinion
about it. Following is the scenario -
A *x = A::Instance() // A is a singleton class, with thread safty
implemented.
A *y = x;
Now, from here x is given to one thread and y to another:
Thread function body of A:
{
x->DoSomething();
delete x;
}
Thread function body of A:
{
while( y->DoSomething() );
}
Question: Is there anything you (a prgrammer) can do to make it safer?
Start by not using obviously unsafe raw pointers.
My Answer: Short answer is "No". Of course I can add reference
counting and only if reference count reaches 0 I will delete the
object (override operator delete). But, there is nothing a programmer
can do that is implicit/compulsory that will avoid the crash.
If you want sharing, bet it among threads or not, you need reference
counting or GC.
And the right way to do it is certainly not by overriding operator
delete.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]