digitalmars.D - Threadsafe singleton using volatile,synchonized
- BLS (28/28) May 26 2007 Probabely I should use D-Learn instead but due to the fact that it is ab...
- Sean Kelly (30/59) May 26 2007 No. The 'volatile' qualifier is applied to a statement, not a variable....
- BLS (13/81) May 26 2007 Thanks Sean, indeed a very interesting thread.
- Sean Kelly (15/26) May 27 2007 In D, 'volatile' is a statement rather than a storage attribute. So you...
- Dejan Lekic (1/7) May 30 2007 synchronized {} does exactly that IMHO.
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: 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: 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
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
May 27 2007
CriticalSectionBegin() { // Your Single thread code goes here } CriticalSectionEnd()synchronized {} does exactly that IMHO.
May 30 2007