Running method while destroying the object

寵の児 提交于 2019-12-05 12:20:13

No, no and no. This is a fundamental design issue, and it shows a common misconception in thinking about multithreaded situations and race conditions in general.

There is one thing that can happen equally likely, and this is really showing that you need an ownership concept: The function calling thread could call the function just right after the object has been destroyed, so there is no object anymore and try to call a function on it is UB, and since the object does not exist anymore, it also has no chance to prevent any interaction between the dtor and a member function.

What you need is a sound ownership policy. Why is the code destroying the object when it is still needed?

Without more details about the code, a std::shared_ptr would probably solve this issue. Depending on your specific situation, you may be able to solve it with a more lightweight policy.

Sounds like a horrible design. Can't you use smart pointer to make sure the object is destroyed only when no-one holds any references to it?

If not, I'd use some external synchronization mechanism. Synchronizing the destructor with a method is really awkward.

There is no methods that can be used to prevent this scenario.

In multithread programming, you need to make sure that an object will not be deleted if there are some others thread still accessing it.

If you are dealing with such code, it needs fundamental fix

(Not to promote bad design) but to answer your two questions:

... deny running the methods, if destructor was called already

You can do this with the solution proposed by @snemarch and @Simon (a lock). To handle the situation where one thread is inside the destructor, while another one is waiting for the lock at the beginning of your method, you need to keep track of the state of the object in a thread-safe way in memory shared between threads. E.g. a static atomic int that is set to 0 by the destructor before releasing the lock. The method checks for the int once it acquires the lock and bails if its 0.

... force the destructor to wait, until any running method is over

The solution proposed by @snemarch and @Simon (a lock) will handle this.

No. Just need to design the program propertly so that it is thread safe.

Why not make use of a mutex / semaphore ? At the beginning of any method the mutex is locked, and the destructor wait until the mutex is unlocked. It's a fix, not a solution. Maybe you should change the design of a part of your application.

Simple answer: no.

Somewhat longer answer: you could guard each and every member function and the destructor in your class with a mutex... welcome to deadlock opportunities and performance nightmares.

Gather a mob and beat some design sense into the 'someone' who thought parallel destruction was a good idea :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!