Running method while destroying the object

前端 未结 8 1576
抹茶落季
抹茶落季 2021-01-12 22:49

A few days ago my friend told me about the situation, they had in their project. Someone decided, that it would be good to destroy the object of NotVerySafeClass

相关标签:
8条回答
  • 2021-01-12 23:37

    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 :)

    0 讨论(0)
  • 2021-01-12 23:39

    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.

    0 讨论(0)
提交回复
热议问题