In a parent-child relationship (virtual machine threads), how can you prevent the parent from mutating as long as there is a child being referred to?


#1

I’m in the process of wrapping a C API that implements a virtual machine for a scripting language. The virtual machine has a main thread, and from that, other child threads can be created and are pushed onto the main thread’s stack. Child threads can themselves have new threads pushed onto their own stacks.

In general, it is not safe to be able to modify the stack of a parent thread while there is still a reference to a child. For example, if the child thread is popped off of the main thread’s stack while there is still an outstanding reference to the child thread, the child thread becomes a dangling reference and the next operation on the child thread usually causes a segfault.

What are some options I have to model this behavior? The ideal solution would introduce minimal runtime overhead, as the whole point of this this particular scripting language is to be lightweight and memory efficient.