Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Always a good idea. Here's what I found. Using your code exactly as written I get
Destroying detach at thread_destruct.pl line 7.
Destroying join at thread_destruct.pl line 7.
Destroying main at thread_destruct.pl line 7.
The first two lines print out immediately, the last about 2 seconds later. None of the objects are assigned to a variable so they are destroyed as soon as they are created. so let's assign them to something:
#!/usr/bin/perl use threads; { package MyObj; sub new { my $class = shift; bless [shift], $class } sub DESTROY { warn "Destroying " , shift->[0]; } } async { my $obj = MyObj->new("detach"); sleep 1 }->detach; async { my $obj = MyObj->new("join"); sleep 2 }->join; my $obj = MyObj->new("main"); sleep 3;
Exact same output, but this time they are printed out with the expected delays. The first one after about one second, the second after two seconds, and the third after about five seconds. The last one takes longer because there's the two second wait waiting for the second async thread to finish and become joinable plus the three second wait in the main thread. However, none of them are destroying during global destruction. Not too surprising, because they are all stored in lexical variables so probably they are destroyed as they go out of scope, before global destruction. Now try assigning them to global variables:
#!/usr/bin/perl use threads; { package MyObj; sub new { my $class = shift; bless [shift], $class } sub DESTROY { warn "Destroying " , shift->[0]; } } async { $obj = MyObj->new("detach"); sleep 1 }->detach; async { $obj = MyObj->new("join"); sleep 2 }->join; $obj = MyObj->new("main"); sleep 3;
This time you get:
Destroying detach at thread_destruct.pl line 7 during global destruction.
Destroying join at thread_destruct.pl line 7 during global destruction.
Destroying main at thread_destruct.pl line 7 during global destruction.
Same timing as the previous version, but now you get the extra phrase at the end of the warning message indicating that you are in the global destruction phase.

Conclusion: Threads go through a global destruction phase individually as they finish execution and are shut down. However, lexical variables are destroyed as they go out of scope because that's when their reference count goes to zero. Global variables don't get destructed until the global destruction phase, which makes it clear why it's called that.

All runs were done with perl 5.10.0 on a 64-bit system running Fedora 12.

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."


In reply to Re^2: Do threads do global destruction? by DrWhy
in thread Do threads do global destruction? by DrWhy

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (5)
    As of 2014-07-23 03:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (132 votes), past polls