Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ object termination notification

In a C++ program, I have two reference counted objects: King and Heir. Heir needs to block until King is destroyed. King is a reference counted object which will be destroyed when it's reference count goes to zero. If Heir holds a reference to King, then King's reference count will never go to zero. How can have Heir block until King is destroyed?

like image 933
brianegge Avatar asked Jul 27 '10 13:07

brianegge


1 Answers

You can use a non-owning (or "weak") reference, similar to how weak_ptr works.

As for waiting until the king is dead, you can use a mutex that the king can hold until he dies and have the heir block waiting for the king to release it.

If you need to have multiple heirs waiting and there is some ordering to the heirs, you can have an "heir selector" object that keeps track of the list of heirs and their order of precedence, and when the king releases the mutex it will assign ownership of that mutex to the next heir in the list.

like image 102
James McNellis Avatar answered Oct 23 '22 00:10

James McNellis