Should destructors be threadsafe?

后端 未结 9 1308
误落风尘
误落风尘 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:36

    If you're accessing global variables you might need thread safety, yes

    eg. My "Window" class adds itself to the list "knownWindows" in the constructor and removes itself in the destructor. "knownWindows" needs to be threadsafe so they both lock a mutex while they do it.

    On the other hand, if your destructor only accesses members of the object being destroyed, you have a design issue.

提交回复
热议问题