Thread overview
Threadsafe singleton using volatile,synchonized
May 26, 2007
BLS
May 26, 2007
Sean Kelly
May 26, 2007
BLS
May 27, 2007
Sean Kelly
May 26, 2007
Probabely I should use D-Learn instead but due to the fact that it is about compiler optimizing ... blah, blah ; Simple question : Is this singleton implementation thread safe ?  Makes :
static volatile Singleton _instance
sense ?

module pattern.singleton

class Singleton
{
public:
	static Singleton getInstance()
	{
		// double check lock
        if (_instance is null)
		{
			synchronized {
                if (_instance is null)
				{
                    _instance = new Singleton();
                }
            }
        }
        return _instance;
    }
private:
	this() {}

	// do not optimize
	static volatile Singleton _instance;
}

Bjoern
May 26, 2007
BLS wrote:
> Probabely I should use D-Learn instead but due to the fact that it is about compiler optimizing ... blah, blah ; Simple question : Is this singleton implementation thread safe ?  Makes : static volatile Singleton _instance
> sense ?
> 
> module pattern.singleton
> 
> class Singleton {
> public:
> 	static Singleton getInstance()
> 	{
> 		// double check lock
>         if (_instance is null)
> 		{
> 			synchronized {
>                 if (_instance is null) 				{
>                     _instance = new Singleton();
>                 }
>             }
>         }
>         return _instance;          }
> private:
> 	this() {} 	
> 	// do not optimize  	static volatile Singleton _instance;
> }


No.  The 'volatile' qualifier is applied to a statement, not a variable.  You need to do something like this:

# class Singleton
# {
#     static Singleton s;
#
#     Singleton instance()
#     {
#         volatile Singleton tmp = s;
#         if(!tmp)
#         {
#            synchronized
#            {
#                tmp = s;
#                if(!tmp)
#                {
#                   volatile tmp = new Singleton();
#                   s = tmp;
#                }
#            }
#         }
#         return s;
#     }
# }

This is from:

http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=9260

in thread:

http://www.digitalmars.com/d/archives/digitalmars/D/9149.html#N9231


Sean
May 26, 2007
Thanks Sean, indeed a very interesting thread.
From 2004;It is really worth to browse the archives!
What is the reason/rationale for using volatile in this way ?

Another Question:
Is something like this available

CriticalSectionBegin()
{
// Your Single thread code goes here
}
CriticalSectionEnd()

Bjoern

Sean Kelly Wrote:

> BLS wrote:
> > Probabely I should use D-Learn instead but due to the fact that it is about compiler optimizing ... blah, blah ; Simple question : Is this singleton implementation thread safe ?  Makes :
> > static volatile Singleton _instance
> > sense ?
> > 
> > module pattern.singleton
> > 
> > class Singleton
> > {
> > public:
> > 	static Singleton getInstance()
> > 	{
> > 		// double check lock
> >         if (_instance is null)
> > 		{
> > 			synchronized {
> >                 if (_instance is null)
> > 				{
> >                     _instance = new Singleton();
> >                 }
> >             }
> >         }
> >         return _instance;
> >     }
> > private:
> > 	this() {}
> > 
> > 	// do not optimize
> > 	static volatile Singleton _instance;
> > }
> 
> 
> No.  The 'volatile' qualifier is applied to a statement, not a variable.
>   You need to do something like this:
> 
> # class Singleton
> # {
> #     static Singleton s;
> #
> #     Singleton instance()
> #     {
> #         volatile Singleton tmp = s;
> #         if(!tmp)
> #         {
> #            synchronized
> #            {
> #                tmp = s;
> #                if(!tmp)
> #                {
> #                   volatile tmp = new Singleton();
> #                   s = tmp;
> #                }
> #            }
> #         }
> #         return s;
> #     }
> # }
> 
> This is from:
> 
> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=9260
> 
> in thread:
> 
> http://www.digitalmars.com/d/archives/digitalmars/D/9149.html#N9231
> 
> 
> Sean

May 27, 2007
BLS wrote:
> Thanks Sean, indeed a very interesting thread.
> From 2004;It is really worth to browse the archives!
> What is the reason/rationale for using volatile in this way ?

In D, 'volatile' is a statement rather than a storage attribute.  So you need to use it explicitly in places where you want to restrict compiler-based code movement.  In the code below, a temporary is used to  ensure that Singleton (s) is fully constructed before s is set, otherwise the compiler could theoretically do this:

s = new Singleton;

// translates to

s = allocate_memory for Singleton;
s.ctor();

The presence of 'volatile' is to ensure that the compiler doesn't try to eliminate 'tmp' entirely and just use 's', since that would be a legal optimization.

> Another Question:
> Is something like this available 
> 
> CriticalSectionBegin()
> {
> // Your Single thread code goes here
> }
> CriticalSectionEnd()

Other than synchronized? Could you provide more detail?


Sean