in reply to Test::More and semaphore handles on Windows

I don't understand something. In

# Test to simulate semaphore growth in Test::Builder. # Grows by 4 semaphore handles per iteration. use strict; use threads; use threads::shared; my @Test_Results = (); share(@Test_Results); sub ok { my $result = {}; share($result); push(@Test_Results, $result); } my $niter = shift or die "usage: $0 number-of-iterations\n"; ok(1) for 1 .. $niter; print "Look at handle count in Windows Task Manager\n"; print "Then press [RETURN] to exit...\n";<STDIN>;

As I understand scoping, $result goes out of scope at the end of the call to ok() so why doesn't the handles get released then? Instead the handles don't seem to be released until perl.exe is finished. I guess this is to do with threads and making sure all shared data is available to all threads.

Replies are listed 'Best First'.
Re^2: Test::More and semaphore handles on Windows
by eyepopslikeamosquito (Archbishop) on Aug 25, 2004 at 06:37 UTC

    No, $result does not get destroyed then because it has been inserted into @Test_Results (which is still in scope) -- perl's reference counting keeps track of this. Running the following program with a large number of iterations, you'll see the handle count go up and up, then it drops back to the original value at the end of the program (when @Test_Results has gone out of scope).

    use strict; use threads; use threads::shared; sub test { my $niter = shift; my @Test_Results = (); share(@Test_Results); for (1 .. $niter) { my $result = {}; share($result); push(@Test_Results, $result); } } my $niter = shift or die "usage: $0 number-of-iterations\n"; test($niter); print "Look at handle count in Windows Task Manager\n"; print "Then press [RETURN] to exit...\n";<STDIN>;