|No such thing as a small change|
Re^2: Do threads do global destruction?by DrWhy (Chaplain)
|on Nov 09, 2012 at 23:10 UTC||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:
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:
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.
"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."