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?

In reply to threads and leaking scalars by deliria

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.