Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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 line 7.
Destroying join at line 7.
Destroying main at 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 line 7 during global destruction.
Destroying join at line 7 during global destruction.
Destroying main at 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.


"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

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, 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
  • 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (5)
    As of 2018-10-17 14:28 GMT
    Find Nodes?
      Voting Booth?
      When I need money for a bigger acquisition, I usually ...

      Results (95 votes). Check out past polls.