Should destructors be threadsafe?

后端 未结 9 1306
误落风尘
误落风尘 2021-02-07 09:08

I was going through a legacy code and found the following snippet:

MyClass::~MyClass()
{
   EnterCriticalSection(&cs);

//Access Data Members, **NO Global**          


        
9条回答
  •  悲&欢浪女
    2021-02-07 09:56

    Define "thread safe". These are possibly the two most ill-understood words in modern computing.

    But if there is a possibility of the destructor being entered twice from two different threads (as the use of symchronisation objects implies) your code is in deep doo-doo. The objects that are deleting the object that you are asking about should be managing this - it is (probably) at that level that synchronisation should be taking place.

提交回复
热议问题