in reply to How to avoid extra DESTROY calls in multi-threaded program?
with two more DESTROY calls, how can I avoid them?
Strictly speaking, you can't. Each thread(including your root thread) has its own private "proxy" copy of the object, which is magically tied to the real object stored in a global shared interpretter context inside threads::shared. When each thread exits, all its object will get DESTROY'd, including the proxies.
I'd suggest you include a member in the object thats the TID of the creator thread to indicate whether the current thread should really DESTROY anything or not. In general, only the object creator should do a full DESTROY. So just add a simple test:
Note that whether you can actually ignore DESTROY in child threads is an app-specific issue; you may need to do some per-thread cleanup on exit.sub new { ...your code... $self->{_creator_tid} = threads->tid(); return bless $self, $class; } sub DESTROY { print "Destroy $_[0]\n" if (threads->tid() == $_[0]->{_creator_tid}); }
|
|---|