N
niry
Why "scalars leaked" output occurs?
My purpose is let three threads all run to the line commented at the
same time(of course, almost the same time).
And is there any better ways to do that?
---output:---
1 Before: 47
2 Before: 47
3 Before: 47
3: 47(0)
1: 47(3)
2: 47(2)
Scalars leaked: 2 #???
Scalars leaked: 2 #???
Scalars leaked: 2 #???
---code:----
use threads qw(yield);
use Thread::Semaphore;
use Time::localtime;
my $semaphore = new Thread::Semaphore;
my $GlobalVariable : shared = 3;
$thr1 = new threads \&sample_sub, 1;
$thr2 = new threads \&sample_sub, 2;
$thr3 = new threads \&sample_sub, 3;
sub sample_sub {
my $SubNumber = shift @_;
$semaphore->down;
$GlobalVariable--;
$semaphore->up;
my $t=0;
$now = localtime();
print "$SubNumber Before:\t".$now->sec."\n";
while ($GlobalVariable>0) {yield;$t++;}
$now = localtime();
print "$SubNumber:\t".$now->sec."($t)\n";
################ all three threads run to here at the same time
}
$thr1->join;
$thr2->join;
$thr3->join;
My purpose is let three threads all run to the line commented at the
same time(of course, almost the same time).
And is there any better ways to do that?
---output:---
1 Before: 47
2 Before: 47
3 Before: 47
3: 47(0)
1: 47(3)
2: 47(2)
Scalars leaked: 2 #???
Scalars leaked: 2 #???
Scalars leaked: 2 #???
---code:----
use threads qw(yield);
use Thread::Semaphore;
use Time::localtime;
my $semaphore = new Thread::Semaphore;
my $GlobalVariable : shared = 3;
$thr1 = new threads \&sample_sub, 1;
$thr2 = new threads \&sample_sub, 2;
$thr3 = new threads \&sample_sub, 3;
sub sample_sub {
my $SubNumber = shift @_;
$semaphore->down;
$GlobalVariable--;
$semaphore->up;
my $t=0;
$now = localtime();
print "$SubNumber Before:\t".$now->sec."\n";
while ($GlobalVariable>0) {yield;$t++;}
$now = localtime();
print "$SubNumber:\t".$now->sec."($t)\n";
################ all three threads run to here at the same time
}
$thr1->join;
$thr2->join;
$thr3->join;