Re: Writing Singleton Classes
Mark A. Gibbs wrote:[color=blue]
>
> DaKoadMunky wrote:
>
>[color=green]
>> One of the respondents to this thread posted an example of the Double
>> Checked
>> Locking idiom.
>>
>> Apparently even that is problematic as discussed by Scott Meyers in the
>> document @ http://www.nwcpp.org/Downloads/2004/DCLP_notes.pdf[/color]
>
>
> am i missing something here? why wouldn't this work:
>
> Singleton* Singleton::Inst ance()
> {
> if (pinstance != 0)
> return pinstance;
>
> Lock();
>
> if (pinstance == 0)
> pinstance = makeSingleton() ;
>
> Unlock();
>
> return pinstance;
> }
>
> Singleton* Singleton::make Singleton()
> {
> return new Singleton;
> }
>
> mark
>[/color]
Andrei Alexandrescu discusses this in "Modern C++ Design". It has to do
with memory architectures. In some cases, you need to do hardware
specific stuff to ensure cache coherency. He also recommends making
pinstance volatile.
Mark A. Gibbs wrote:[color=blue]
>
> DaKoadMunky wrote:
>
>[color=green]
>> One of the respondents to this thread posted an example of the Double
>> Checked
>> Locking idiom.
>>
>> Apparently even that is problematic as discussed by Scott Meyers in the
>> document @ http://www.nwcpp.org/Downloads/2004/DCLP_notes.pdf[/color]
>
>
> am i missing something here? why wouldn't this work:
>
> Singleton* Singleton::Inst ance()
> {
> if (pinstance != 0)
> return pinstance;
>
> Lock();
>
> if (pinstance == 0)
> pinstance = makeSingleton() ;
>
> Unlock();
>
> return pinstance;
> }
>
> Singleton* Singleton::make Singleton()
> {
> return new Singleton;
> }
>
> mark
>[/color]
Andrei Alexandrescu discusses this in "Modern C++ Design". It has to do
with memory architectures. In some cases, you need to do hardware
specific stuff to ensure cache coherency. He also recommends making
pinstance volatile.
Comment