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:

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}); }
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.

Perl Contrarian & SQL fanboy

In reply to Re: How to avoid extra DESTROY calls in multi-threaded program? by renodino
in thread How to avoid extra DESTROY calls in multi-threaded program? by jwu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.