Kam
Kam

Reputation: 6008

An object acquiring mutex in destructor to prevent deletion of itself

Can this concept work?

class MyClass
{
public:
    ~MyClass()
     {
         MyMutex.acquire();
     }

     void ThreadFunction(void* param)
     {
          MyMutex.acquire();
          //do something
          MyMutex.release();
     }

};

Also Let's say we have an object of this class, call it "inst"

What I am trying to achieve is that if:

Is that ok to do?

Upvotes: 0

Views: 343

Answers (1)

woolstar
woolstar

Reputation: 5083

It would be much better to create a wrapper around MyClass if MyClass contains resources. Its even worse if the program uses a class that inherits from MyClass, because the destructor for ChildofMyClass will have already been called by this point.

Upvotes: 3

Related Questions