digitalmars.D.learn - Singleton Pattern
- asm (1/1) Jan 05 2012 how can i implementing the singleton pattern in D?
- Andrew Wiley (2/3) Jan 05 2012 Multithreaded or not?
- asm (1/1) Jan 05 2012 both if you don't mind
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/5) Jan 05 2012 Is singleton still alive? ;)
- Suliman (3/9) Jul 09 2016 Yeah, same question, what difference between singleton and
- =?UTF-8?Q?Ali_=c3=87ehreli?= (8/17) Jul 09 2016 this()`?
- Suliman (6/26) Jul 09 2016 Yeah, I understand, I mean that I need way to create instance on
- Jonathan M Davis (55/56) Jan 05 2012 The same way that you would in C++ or Java, except that unlike you have ...
- asm (1/1) Jan 06 2012 thank you, you answer is very helpful.
- Thomas Mader (4/5) Jan 11 2012 static
- Jonathan M Davis (10/17) Jan 11 2012 No, because enums must be known at compile time. This avoids the orderin...
- yawniek (2/3) Jul 10 2016 https://p0nce.github.io/d-idioms/#Leveraging-TLS-for-a-fast-thread-safe-...
On Thu, Jan 5, 2012 at 4:15 PM, asm <asm hotmail.com> wrote:how can i implementing the singleton pattern in D?Multithreaded or not?
Jan 05 2012
On 01/05/2012 02:15 PM, asm wrote:how can i implementing the singleton pattern in D?Is singleton still alive? ;) An idea is to instantiate the object in the module's static this(). Ali
Jan 05 2012
On Thursday, 5 January 2012 at 22:53:12 UTC, Ali Çehreli wrote:On 01/05/2012 02:15 PM, asm wrote:Yeah, same question, what difference between singleton and `static this()`?how can i implementing the singleton pattern in D?Is singleton still alive? ;) An idea is to instantiate the object in the module's static this(). Ali
Jul 09 2016
On 07/09/2016 01:35 PM, Suliman wrote:On Thursday, 5 January 2012 at 22:53:12 UTC, Ali Çehreli wrote:I'm glad I was alive in 2012. :o)On 01/05/2012 02:15 PM, asm wrote:how can i implementing the singleton pattern in D?Is singleton still alive? ;)this()`? I'm now aware of the difference between 'static this()' and 'shared static this()'.: shared static this(): Executed once for the whole program static this(): Executed per thread AliAn idea is to instantiate the object in the module's static this(). AliYeah, same question, what difference between singleton and `static
Jul 09 2016
On Saturday, 9 July 2016 at 20:47:32 UTC, Ali Çehreli wrote:On 07/09/2016 01:35 PM, Suliman wrote:Yeah, I understand, I mean that I need way to create instance on Config class, that should not be passed in every instance of class explicitly, because it would need in every class. I know that I can make it's static, but I do not think, that it's good. Same with singleton, I read that a lot of people blame it.On Thursday, 5 January 2012 at 22:53:12 UTC, Ali Çehreliwrote:I'm glad I was alive in 2012. :o)On 01/05/2012 02:15 PM, asm wrote:how can i implementing the singleton pattern in D?Is singleton still alive? ;)this().An idea is to instantiate the object in the module's static`static this()`? I'm now aware of the difference between 'static this()' and 'shared static this()'.: shared static this(): Executed once for the whole program static this(): Executed per thread AliAliYeah, same question, what difference between singleton and
Jul 09 2016
On Thursday, January 05, 2012 22:15:32 asm wrote:how can i implementing the singleton pattern in D?The same way that you would in C++ or Java, except that unlike you have static constructors that you can use to initialize them, and unlike both, if it's not shared, then it's a singleton per thread and no locking is required. If you're dealing with a multi-threaded singleton, technically, it should be possible to use double-checked locking with shared, but apparently bugs with shared make that unsafe at present. So, if you didn't initialize it in a shared static constructor, either you lock every time that you fetch the singleton or you use an extra bool for indicating that it's been initialized. Single threaded: T singleton() { static T instance; if(!instance) instance = new T(); return instance; } Multi-threaded: shared(T) singleton() { static shared T instance; static bool initialized = false; if(!initialized) { synchronized { if(!instance) instance = new shared(T)(); } initialized = true; } return instance; } Once the bugs with shared with regards to fences have been fixed though, the shared version can look more like shared(T) singleton() { static shared T instance; if(!instance) { synchronized { if(!instance) instance = new shared(T)(); } } return instance; } Regardless, if you use a shared static constructor, you can avoid the locking (it's just not lazily initialized then), and of course, the singly threaded one can use a static constructor if you don't care about lazy initialization. If you use a static constructor though, the instance would not be declared inside the singleton function (and it doesn't have to be here either - it's just shorter to do it this way in an example). - Jonathan M Davis
Jan 05 2012
On Friday, January 6, 2012, Jonathan M Davis <jmdavisProg gmx.com> wrote:The same way that you would in C++ or Java, except that unlike you havestatic The most simple and elegant way in java is to use an enum. Is there nothing comparable in D?
Jan 11 2012
On Thursday, January 12, 2012 00:55:14 Thomas Mader wrote:On Friday, January 6, 2012, Jonathan M Davis <jmdavisProg gmx.com> wrote:No, because enums must be known at compile time. This avoids the ordering issues that you get in languages like C++ or Java when you directly initialize static variables like that. So, to have an enum which is a class, you need to be able to construct a class at compile time and have it persist into the runtime of the program. And while you _can_ now use classes with CTFE, they cannot persist beyond CTFE (though that may be possible eventually). Any class which is used at runtime must be constructed at runtime. So, either you need to use a static constructor, or you need to initialize the singleton lazily. - Jonathan M DavisThe same way that you would in C++ or Java, except that unlike you havestatic The most simple and elegant way in java is to use an enum. Is there nothing comparable in D?
Jan 11 2012
On Thursday, 5 January 2012 at 22:15:32 UTC, asm wrote:how can i implementing the singleton pattern in D?https://p0nce.github.io/d-idioms/#Leveraging-TLS-for-a-fast-thread-safe-singleton
Jul 10 2016