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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: threads and leaking scalars
by liz (Monsignor) on Aug 04, 2003 at 08:03 UTC | |
by deliria (Chaplain) on Aug 04, 2003 at 12:20 UTC | |
|
Re: threads and leaking scalars
by ikegami (Patriarch) on Mar 21, 2008 at 01:13 UTC | |
|
Re: threads and leaking scalars (Scalars leaked: 1)
by dpmott (Scribe) on Feb 26, 2004 at 18:09 UTC | |
by ikegami (Patriarch) on Mar 21, 2008 at 01:09 UTC | |
by myuserid7 (Scribe) on Mar 21, 2008 at 00:37 UTC | |
|
Re: threads and leaking scalars
by Monkomatic (Sexton) on Mar 08, 2011 at 00:47 UTC | |
|
Re: threads and leaking scalars
by Monkomatic (Sexton) on Mar 08, 2011 at 00:37 UTC |