a simple thread testing program produces some very strange results:could you please tell me what the

A

Asfand Yar Qazi

#!/usr/local/bin/perl -w

package Thread_Maker;

use strict;

sub new()
{
my $proto = shift();
my $class = ref($proto) || $proto;
my $self = [];
bless($self, $class);
return $self;
}

sub joinall()
{
my $self = shift;
foreach my $thread (@$self) {
print("Joining thread\n");
$$thread->join();
}
}

sub add(\@\&@)
{
my ($self, $funcref, @args) = @_;
my $tmpthread = threads->new($funcref, @args);
push(@$self, \$tmpthread);
}

1;

package main;

use Config;
use threads;
use threads::shared;
use Gtk;
use strict;

$Config{useithreads} or die "Recompile Perl with threads to run this
program.";

my $a : shared = 0;

sub sub1($\$)
{
my ($sleeptime, $var) = @_;
sleep($sleeptime);
++$$var;
}

sub printer(\$)
{
my ($var) = @_;
my $oldvar = $var;
while($$var != 2) {
while($$var == $oldvar) {}
$oldvar = $$var;
print('Variable = ', $$var, "\n");
};
print("Now exiting\n");
}

my $tmaker = new Thread_Maker;

$tmaker->add(\&printer, \$a);
$tmaker->add(\&sub1, 1, \$a);
$tmaker->add(\&sub1, 2, \$a);

$tmaker->joinall();

--OUTPUT--

Variable = 0
Joining thread
Variable = 1
Variable = 2
Now exiting
Joining thread
Joining thread
Scalars leaked: 1
Scalars leaked: 1
Scalars leaked: 1

--END OUTPUT--

What's with the 'Scalars leaked: 1' stuff?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top