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

Re: Do threads do global destruction?

by choroba (Abbot)
on Nov 09, 2012 at 21:44 UTC ( #1003208=note: print w/ replies, xml ) Need Help??


in reply to Do threads do global destruction?

T.I.T.S. Or, Try It To See..

#!/usr/bin/perl use threads; { package MyObj; sub new { my $class = shift; bless [shift], $class } sub DESTROY { warn "Destroying ", shift->[0] } } async { MyObj->new("detach"); sleep 1 }->detach; async { MyObj->new("join"); sleep 2 }->join; MyObj->new("main"); sleep 3;
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Do threads do global destruction?
Download Code
Re^2: Do threads do global destruction?
by DrWhy (Chaplain) on Nov 09, 2012 at 23:10 UTC
    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..."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003208]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2014-10-23 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls