Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Do threads do global destruction?

by DrWhy (Chaplain)
on Nov 09, 2012 at 18:02 UTC ( [id://1003193]=perlquestion: print w/replies, xml ) Need Help??

DrWhy has asked for the wisdom of the Perl Monks concerning the following question:

I've been looking through documentation and googling, but can't seem to find a clear answer to this question.

When a thread is joined does it go through a global destruction cycle for all the objects that were local to it or is there only a single global destruction phase at the end of the entire perl process?

--DrWhy

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

Replies are listed 'Best First'.
Re: Do threads do global destruction?
by dave_the_m (Monsignor) on Nov 09, 2012 at 21:42 UTC
    Each thread has its PL_perl_destruct_level variable set to 2; this is the internal equivalent of the PERL_DESTRUCT_LEVEL environment variable. The net effect is that when the thread is being freed after joining, all data structures associated with that thread are freed (modulo bugs).

    Dave.

Re: Do threads do global destruction?
by choroba (Cardinal) on Nov 09, 2012 at 21:44 UTC
    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;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      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..."

Re: Do threads do global destruction?
by zentara (Cardinal) on Nov 09, 2012 at 19:06 UTC
      I don't think the link you gave has any information relevant to my question. I'm asking about the global destruction phase of the perl interpreter and whether the mark and sweep destruction of objects that happens as part of that happens at the end of each thread inside a perl process or only at the end of the entire perl process. This link seems to have more to do with memory leaks which are unrelated to my current issue.

      --DrWhy

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

        I don't think the link you gave has any information relevant to my question

        Sure it does, it contains code that demonstrates that memory is reclaimed after a thread ends (join) -- "global destruction" must have happened

Re: Do threads do global destruction?
by rjt (Curate) on Nov 09, 2012 at 22:30 UTC

    You already have some worthy replies to your question, but I thought I'd take a run at a more visual presentation. I create a bunch of threads that each create a large-ish array of random numbers and return after a short delay.

    After about 30 minutes on my 64-bit linux system it was clear the memory was not increasing; it hit its maximum within the first several seconds of the test while the threads were spooling up:

    Memory leak test. Ctrl-C to stop. 20 threads (17831 lifetime), 845.99 MiB RSS [================ ] / ^C

    Code below. Certainly not my most elegant work. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1003193]
Approved by herveus
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2025-02-07 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (94 votes). Check out past polls.