views:

309

answers:

4

We have a static (singleton) class which will be used in a mutithreaded environment. We use mutex in its constructor and other mrmber functions. However there is no mutex for the destructor. Destructor do some tasks like cleaning up of some other member objetcs etc. Do we need to a mutex in the distructor also ?

+2  A: 

In terms of accessing the object the destructor is destructing, no, it shouldn't use a mutex. The calling code is responsible for ensuring that the destructor is called once, and once only; that is not the destructor's responsibility.

In terms of accessing any other data or resources, there is nothing special about accessing them from a destructor. They will or will not need protecting with mutexes the same whether they are called from this destructor or any ordinary function.

Dave Hinton
A: 

If you have multiples thread that try to delete an object, then your design is seriously wrong.

You have to use syncronization only in the member function that gives to you the unique instance because it makes sense to have multiple objects requesting an instance, but there should be only one point where you decide to drop the object and don't use it anymore.

akappa
+1  A: 

The same rationale that Dave gives for the destructor applies to the constructor. While the object is still being constructed it should not be accessed by any other thread. The constructor is guaranteed to be run only by one thread (you cannot contruct twice) but you must warrant that no thread access the object before it is fully constructed.

Internal locks should never be the responsability of constructors or destructors for data access. They can be used in destructors for thread synchronization: if an object is already inside the object (a thread is running one of your object methods) as to guarantee that you will not delete the object in the middle of an operation.

That is a general recipe for all types of classes with constructor and destructor used in the language sense. Now, with singletons there scenario is a little more specific. Usually when you talk about constructor of a singleton you are in fact talking about the static method that creates the object, but not the real constructor. That static method usually will have synchronization mechanisms that can include a static mutex.

Before getting deeper into discussions of what is destruction of a singleton you should state what is your intended use. Will the singleton be destroyed and recreated many times during the application lifetime? Will it be created once and live forever? How are you implementing the singleton? Those questions are the start point to decide if, when and where you need synchronization primitives.

David Rodríguez - dribeas
A: 

If the destructor modifies some global data ( like global counter etc), or some shared resource then you need mutex. Otherwise guarding destructor as such means its design issue. At no time thread should try to delete the object when object is in use by some other thread.

aJ