The problem arises because the destructor for the shared object is being called from each thread that has a copy of it.
One way to avoid that is to add your own destructor method and defer destruction until the called by the last thread that has a copy.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |
| [reply] |
| [reply] |
Patience is a virtue.
With problems of this nature you'd get a more (and faster) responses if you posted a runnable example. As is, you force anyone thinking of trying to help, to have to try to recreate the code that you already have sitting on your harddrive. Most won't bother. I tried and failed.
WIAT: Modifying your OP without attribution, such that it makes my initial response read as wrong, is not the best way to endear yourself.
I cannot find the discussion I remember concerning DESTROY methods and threads, despite several searches. However, it may not be applicable as I have a feeling that half-remembered discussion was XS related.
However, two possibilities come to mind:
- Avoid closures by reformatting your code thus:
use sshConnector;
sub methodA {
my $self = shift;
my $count = shift;
my $hostname;
while ($count) {
($status,$hostname) = $self->cmd("hostname");
print "$count : $hostname\n";
sleep 1;
$count--;
}
}
sub methodB {
my $self = shift;
my $count = shift;
my $hostname;
while ($count) {
($status,$hostname) = $self->cmd("hostname");
print "$count : $hostname\n";
sleep 2;
$count--;
}
}
my $machine1 = new Connector($deviceInfo1);
my $thr1 = threads->create('methodA',$machine1,10);
my $machine2 = new Connector($deviceInfo2);
my $thr2 = threads->create('methodB',$machine2,6);
$thr1->join();
print "First thread over\n";
$thr2->join();
print "Second thread over\n";
Closures and threads have a history of bugs--many fixed--but still best avoided if possible.
- Try marking your objects shared explicitly:
my $machine1 :shared = new Connector($deviceInfo1);
my $thr1 = threads->create('methodA',$machine1,10);
my $machine2 :shared = new Connector($deviceInfo2);
my $thr2 = threads->create('methodB',$machine2,6);
On the off-chance that the relatively recent changes that made it possible to shared blessed references also added some magic to ensure the appropriate ordering of destructors.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |