Does a static class destructor need a mutex?

We have a static (singleton) class that will be used in mutithreaded. We use the mutex in its constructor and other mrmber functions. However, there is no mutex for the destructor. The destructor performs some tasks, such as cleaning up some other object objects, etc. Do we also need to use the mutex in the distructor?

+3
source share
4 answers

From the point of view of access to the object, the destructor destroys, no, it should not use the mutex. The calling code is responsible for the fact that the destructor is called once and once; it is not the responsibility of the destructor.

, . , , .

+3

, , . , . ( ), , .

. : ( ), , .

, . , , . , , , , . , .

, , , . ? ? ? , , .

+2

, , .

syncronization -, , , , , .

0

If the destructor changes some global data (for example, a global counter, etc.) or some shared resource, you need a mutex. Otherwise, protecting the destructor as such means its design problem. In no case should a thread attempt to delete an object when the object is being used by another thread.

0
source

Source: https://habr.com/ru/post/1709581/


All Articles