January 8th, 2010, 3:04 pm
QuoteOriginally posted by: dobranszkyI made the following simple implementation of Singleton. I know that Double-Checked Locking (DPL) may fail in theory, but using a temp variable I have never experienced any problem (I use the Intel C++ compiler on Windows).I use the singleton pattern for my issue logger like Singleton<IssueLog>::Instance().Add<eDebug>(dataToLog); I change the verbosity level of my logger in runtime. If the verbosity level is not reached, thanks to DPL the logging code has basically no overheat. In general I log issues from OpenMP threads.I put px into the function. Therefore, I can forecast when my logger object is destructed. At the time of destruction I serialize the issues using Boost that has itself static variables initialized at the load of the DLL. Using scoped_ptr my logger is automatically destructed and the issues are serialized when I unload my DLL.An alternative could be to use non-thread-safe singletons and force them to instantiate the underlying object when the DLL is loaded. At that time there is usually only one thread active. Therefore, there is no need for a mutex or for DPL. An example for this can be found in boost/serialization/singleton.hpp. However, in this case in the constructor of the underlying object we have to reference all other static objects and singletons that we would like to destruct strictly after that the destruction of our underlying object has already finished. For instance, in my case in the constructor of IssueLog I have to reference the Boost Serialization singletons, otherwise I cannot serialize the issues when my IssueLog is destructed. Sometimes, it may be difficult to discover each dependence.Is there a fundamental reason for using scoped pointers? A shared pointer is a better metaphor imo because multiple objects access it and it lives until no longer needed.QuoteFor instance, in my case in the constructor of IssueLog I have to reference the Boost Serialization singletons, otherwise I cannot serialize the issues when my IssueLog is destructed. Sometimes, it may be difficult to discover each dependence.A possible useful feature is that they have 'custom deleters' by using function objects so thar resource deallocation is controllled. It is called even when an exception occcurs.I am wondering if using boost::any would be better than template arguments...
Last edited by
Cuchulainn on January 7th, 2010, 11:00 pm, edited 1 time in total.