http://www.perlmonks.org?node_id=280529

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

I started playing around with threads in perl5.8 and came across something I can't seem to get my head around.

In the following piece of code I call 'new My_mod( \%config )'. I figured this would work, however Perl complains about 1 leaked scalar for each of the threads started. If I don't pass it an argument it does not leak scalars.

I figured 5.8.1 might do better, so i compiled perl-5.8.1rc4 and had it run the example, but the problem remained.

It's fairly easy to work around this problem, but i'd like to understand why it leaks scalars, or where i'm doing something wrong.

#!/usr/local/bin/perl package My_mod; use warnings; use strict; use threads; use threads::shared; our $thread_abort : shared = 0; our $thread_continue : shared = 0; sub new { my ( $pkg,$config ) = @_; for ( 0..4 ) { threads->new( \&test_t ); } bless { }, $pkg; } sub test_t { my $iter = 0; while ( 1 ) { threads->yield; last if $thread_abort; next unless $thread_continue; ++$iter; } print "My iter count : $iter\n"; } sub activate { my $pkg = shift; $thread_continue = 1; } sub abort { my $pkg = shift; $thread_abort = 1; for my $thr ( threads->list ) { if ( $thr->tid && !($thr == threads->self ) ) { $thr->join } } } package main; my %config = ( 'test' => '3' ); my $node = new My_mod( \%config ); $node->activate; sleep(10); $node->abort;
Any thoughts?