-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better Singleton Support? #64
Comments
On Mar 29, 2013, at 7:10 AM, "Benjamin S. Kirk" [email protected] wrote:
I imagine you will need multiple inheritance to do this. And how about the actual destructor of said object? Should it check to ensure that it's already been torn down?
|
On Mar 29, 2013, at 8:50 AM, "jwpeterson" [email protected] wrote:
Ah, yes - we don't but we could... I mean, right now we are working around that sort of - the reference counting foo is a singleton that we are deriving singleton objects from... Seems like a ubiquitous problem - anyone aware if there is a boost-ish like solution? -Ben |
Withdrew my solution for now because it had a memory leak ;-) Currently fixing ;-) |
I'm not, but that doesn't mean there isn't something Boost-ish that solves
|
On Mar 29, 2013, at 9:16 AM, Derek Gaston [email protected] wrote:
I didn't realize the reverse order was guaranteed?
Yes and No (below…)
The only reason the order matters is because of Singletons derived from Singletons - the RemoteElem : ReferenceCountedObject business. Although provided we can guarantee the reference counting foo is initialized before any static Elem singletons, and your reverse destruction is guaranteed, I think that is enough…? -Ben |
Ok here's my solution again... but it's really not all that cool now. Basically the base class just registers each singleton automatically as they are created... and then we can delete them later in a guaranteed reverse order. This has the advantage that you won't ever create a singleton and forget to add it to the tear down list. // Deleted in favor of my final solution |
You could do this without needing tearDown as well now... just use static pointers instead and store pointers instead (similar to my first solution) then delete them in reverse order. |
Ok - here's my final solution:
|
On Mar 29, 2013, at 9:40 AM, "Derek Gaston" [email protected] wrote:
I like it. If there is general agreement this would be a Good Thing then ill leave this issue open and implement/refactor after my current other feature blitz subsides. -Ben |
On Fri, Mar 29, 2013 at 8:44 AM, Benjamin S. Kirk
I definitely don't agree with adding infinite loops to the code... int main() for(unsigned int i=_singletons.size()-1; i != -1; i--) } |
On Mar 29, 2013, at 10:01 AM, "jwpeterson" [email protected] wrote:
Good place for an iterator... |
On Fri, Mar 29, 2013 at 8:23 AM, Benjamin S. Kirk
I don't think it is unless the compiler can discern the dependencies "If the constructors of a, b and c use ans, you should normally be John |
@jwpeterson Whoops! "unsigned" doh.... lol. Yeah just use an iterator... ;-) |
I really like Derek's last solution as modified by either John's bug I've been trying to think of cons but so far my only concern is "will This looks good. |
Implemented in dde02a2 |
Ok, the discussion in #62 and fear of the alternate implmentation got me thinking that if we had better core support for Singletons inside the library the alternative may not be so bad...
Right now the only one we really have is RemoteElem. I've introduced more in #62, so it seems maybe we need a more general way to handle these.
Ideally singletons can be created as static data inside a class, or as a static object inside a function. The latter allows you to control when the singleton is created:
The nice thing about that is the singleton is not created until needed. Unfortunately the destruction is still a mystery (to me anyway?), and problematic if the Singleton is a reference counted object.
Our current approach is to create singletons in LibMeshInit() and destroy them in ~LibMeshInit(). I like this, but rather than have LibMeshInit() ultimately know and manage all singletons, what if instead it contained a list of pointers to LibMeshSingleton objects or something?
The LibMeshInit destructor loops over all objects (that were added somewhere else) and calls tear_down(); - thereby allowing a predictable and safe destruction order, while still allowing singletons to get created only at the time of first access, which in some cases could be never?
In the RemoteElem implementation then, for example, there is a simple
@roystgnr, @jwpeterson, @friedmud -- thoughts?
The text was updated successfully, but these errors were encountered: