Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Strange behaviour with "do" and Perl 5.8

Reply
Thread Tools

Strange behaviour with "do" and Perl 5.8

 
 
Steve D
Guest
Posts: n/a
 
      08-26-2003
Below is some code being used to test threads. I'm trying to move
some code into separate files and use "do <file>" to run the code.
When I try this, I get strange behaviour. It is likely some isuee of
variable visibility, but I can't figure it out. Help please. (All
results are on Win2K with ithreads).
Regards,
Steve

The code is Q.pl (below).

When it is run as is, it runs fine, producing:
Sleeper 2, iteration 0, error 0.00000, current time 0.00000
Sleeper 3, iteration 0, error 0.00000, current time 0.00000
Sleeper 5, iteration 0, error 0.00000, current time 0.00000
Sleeper 7, iteration 0, error 0.00000, current time 0.00000
Sleeper 11, iteration 0, error 0.00000, current time 0.00000
Sleeper 2, iteration 1, error 0.00313, current time 0.20313
Sleeper 3, iteration 1, error 0.01250, current time 0.31250
Sleeper 2, iteration 2, error 0.00625, current time 0.40625
Sleeper 5, iteration 1, error 0.00000, current time 0.50000
Sleeper 2, iteration 3, error 0.00938, current time 0.60938
Sleeper 3, iteration 2, error 0.00938, current time 0.60938
Sleeper 7, iteration 1, error 0.00313, current time 0.70313
Sleeper 2, iteration 4, error 0.01251, current time 0.81251
Sleeper 3, iteration 3, error 0.00626, current time 0.90626
Sleeper 5, iteration 2, error 0.00001, current time 1.00001
Sleeper 2, iteration 5, error 0.00001, current time 1.00001
Sleeper 11, iteration 1, error 0.00938, current time 1.10938
Sleeper 2, iteration 6, error 0.00313, current time 1.20313
Sleeper 3, iteration 4, error 0.00313, current time 1.20313
Sleeper 7, iteration 2, error 0.00626, current time 1.40626
Sleeper 2, iteration 7, error 0.00626, current time 1.40626
Sleeper 5, iteration 3, error 0.00001, current time 1.50001
Sleeper 3, iteration 5, error 0.00001, current time 1.50001
Sleeper 2, iteration 8, error 0.00939, current time 1.60939
Sleeper 3, iteration 6, error 0.01251, current time 1.81251
Sleeper 2, iteration 9, error 0.01251, current time 1.81251
Sleeper 5, iteration 4, error 0.00001, current time 2.00001
Sleeper 2, iteration 10, error 0.00001, current time 2.00001

If you comment out the Q1 and Q2 sections, and use the "do" file
commands to run the same code (now located in Q1.pl and Q2.pl), the
program produces the same (correct) output except it also prints (when
exiting):
Attempt to free unreferenced scalar during global destruction.
Scalars leaked: 1
Scalars leaked: 1
Scalars leaked: 1
Scalars leaked: 1
Scalars leaked: 1

If only one of the "do" file commands is used (and the other left as
explict code) then there are complaints about initialization, e.g. if
Q1 is left inline, but Q2 is done as a "do 'Q2.pl';", it produces:
Sleeper 2, iteration 0, error 0.00000, current time 0.00000
Sleeper 3, iteration 0, error 0.00000, current time 0.00000
Sleeper 5, iteration 0, error 0.00000, current time 0.00000
Sleeper 7, iteration 0, error 0.00000, current time 0.00000
Sleeper 11, iteration 0, error 0.00000, current time 0.00000
Use of uninitialized value in numeric eq (==) at C:/Program
Files/Perl/lib/threads.pm line 71.
Use of uninitialized value in numeric eq (==) at C:/Program
Files/Perl/lib/threads.pm line 71.
Use of uninitialized value in numeric eq (==) at C:/Program
Files/Perl/lib/threads.pm line 71.
Use of uninitialized value in numeric eq (==) at C:/Program
Files/Perl/lib/threads.pm line 71.
Use of uninitialized value in numeric eq (==) at C:/Program
Files/Perl/lib/threads.pm line 71.
A thread exited while 6 other threads were still running.

============= Code for Q.pl
use strict ;
use warnings;

use Time::HiRes qw(gettimeofday) ;
use threads ;

sub delay ($) { select ( undef, undef, undef, $_[0] ) } ;

sub Timer {
my ( $timeout ) = @_ ;
my $basetime = gettimeofday ;
for my $c ( 0 .. 10/$timeout ) {
my $current = gettimeofday - $basetime ;
my $error = $current - $c*$timeout/10 ;
printf
"Sleeper %3d, iteration %3d, error %2.5f, current time
%4.5f\n" ,
$timeout, $c, $error, $current ;
my $sleep = ($c+1)*$timeout/10 - gettimeofday + $basetime ;
delay $sleep ;
} ;
}

my %threads ;

#
do 'Q1.pl' ;

# Q1.pl contains this, and only this:
# foreach my $timeout ( 2, 3, 5, 7, 11 ) {
# my $t = threads->new( \&Timer, $timeout );
# # Can't store a thread object directly into Client_Data,
# #so just store the thread ID
# $threads{$timeout} = $t->tid ;
# }


#
# do 'Q2.pl' ;

# Q2.pl contains this, and only this
foreach my $timeout ( 2, 3, 5, 7, 11 ) {
threads->object($threads{$timeout})->join ;
}
__END__
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strange behaviour of getElementsByName and method name in IE and FF Thomas 'PointedEars' Lahn Javascript 3 11-19-2009 03:05 PM
New to Perl - strange behaviour with backticks Eric Perl Misc 2 09-13-2007 10:02 PM
debugger behaviour different to execution behaviour Andy Chambers Java 1 05-14-2007 09:51 AM
Strange printing behaviour of Perl programs Nikita Synytskyy Perl Misc 6 01-20-2004 09:31 PM
Strange behaviour with perl and apache David Cantin Perl 1 11-03-2003 11:56 PM



Advertisments