
<Jason Hise wrote>
Yay, I finally got the singleton working with allocation and lifetime policies, in addition to being in CRTP form. I know that threading policies still remain, however I would like an opinion on what I have so far. A major difference from Loki is that my lifetime policy needs to know about the allocation policy in order for dependencies to be possible to implement. The code is available here:
Jason, Great work - I've been away from the list for a couple of days so sorry I didn't get back sooner, but I think you have definitely gone the right way with what you've done up to now. My recommendations for things to look at include: 1) Allocate using malloc as another creation policy 2) Longevity lifetimes 3) Possibly include the ability to turn off phoenix singletons in the dependency lifetime policy. 4) Threading. With all of those, you've almost certainly exceed Loki - especially if you can do them without have to link to any external code. Dave Handley

Dave Handley wrote:
Great work - I've been away from the list for a couple of days so sorry I didn't get back sooner, but I think you have definitely gone the right way with what you've done up to now.
Thanks :)
My recommendations for things to look at include:
1) Allocate using malloc as another creation policy 2) Longevity lifetimes
I'm actually working on the longevity policy now. I'm trying to do it without using atexit, and instead using a hidden internal singleton that manages the lifetimes of the others. It should be interesting.
3) Possibly include the ability to turn off phoenix singletons in the dependency lifetime policy.
This leads to an interesting question: what should happen if a class tries to get access to a destroyed singleton? I would personally think that it would always be desirable to have it come back, but then this is just my opinion and probably falls into the same category as me preferring dependencies to longevities. Which leads me to wonder if OnDeadReference should be an additional policy of the lifetime policy itself.
4) Threading.
I'm going to need help with this one. My current thought is to provide a lock policy. The lock would be required to provide locking upon creation and unlocking upon destruction, and then all the other policies of the singleton would inherit this policy and could simply insert lock objects wherever they are needed. The default single threaded lock would just be an empty class. Unfortunately I have no experience with how threading actually works, and I am worried that the two threads would just end up creating two different locks and neither would block the other. If I am not on the right track, please point me in the right direction. -Jason

Jason Hise wrote: [...]
If I am not on the right track, please point me in the right direction.
Try http://groups.google.de/groups?selm=3EC3AA39.7126DCCC%40web.de http://groups.google.de/groups?selm=3ECBD751.8DE8AED5%40web.de http://groups.google.de/groups?selm=4049DF28.BFEDCD0A%40web.de http://groups.google.de/groups?selm=410280D4.F18B3B87%40web.de http://groups.google.de/groups?selm=412533CA.82703A04%40web.de http://groups.google.de/groups?selm=41C301DF.5B50EDB7%40web.de regards, alexander.

Alexander Terekhov wrote:
http://groups.google.de/groups?selm=3EC3AA39.7126DCCC%40web.de http://groups.google.de/groups?selm=3ECBD751.8DE8AED5%40web.de http://groups.google.de/groups?selm=4049DF28.BFEDCD0A%40web.de http://groups.google.de/groups?selm=410280D4.F18B3B87%40web.de http://groups.google.de/groups?selm=412533CA.82703A04%40web.de http://groups.google.de/groups?selm=41C301DF.5B50EDB7%40web.de
Thanks for the links. However, I am not looking to use a threading library... instead, I only want to provide a hook for client code to use. What I am mainly looking for is a good interface for a thread safety mechanism. Then I can let client code deal with the details. Is requiring empty lock and unlock methods adequate, or are there other safety mechanisms that should be included? -Jason

Dave Handley wrote:
My recommendations for things to look at include:
1) Allocate using malloc as another creation policy
I'm looking at Loki's library and thinking that it has a major bug. Doesn't his implementation have the same alignment problems that my original static allocator did? Wouldn't it be impossible to ensure that memory allocated by malloc was aligned properly? -Jason

Jason Hise wrote:
Dave Handley wrote:
My recommendations for things to look at include:
1) Allocate using malloc as another creation policy
I'm looking at Loki's library and thinking that it has a major bug. Doesn't his implementation have the same alignment problems that my original static allocator did? Wouldn't it be impossible to ensure that memory allocated by malloc was aligned properly?
The memory allocated by malloc and ::operator new is always properly aligned for any type (sometimes excluding nonstandard types such as __m128).

"Jason Hise" wrote:
I'm looking at Loki's library and thinking that it has a major bug. Doesn't his implementation have the same alignment problems that my original static allocator did? Wouldn't it be impossible to ensure that memory allocated by malloc was aligned properly?
Memory returned by malloc/realloc/calloc is always aligned to fit any data type. /Pavel

Pavel Vozenilek wrote:
Memory returned by malloc/realloc/calloc is always aligned to fit any data type.
Peter Dimov wrote:
The memory allocated by malloc and ::operator new is always properly aligned for any type (sometimes excluding nonstandard types such as __m128).
Ok, thanks, thats good to know. -Jason
participants (5)
-
Alexander Terekhov
-
Dave Handley
-
Jason Hise
-
Pavel Vozenilek
-
Peter Dimov