1

我是线程新手,这就是我所做的:

my $thread_fifo = threads->create(sub {Plugins::Fifo->run($conf, $products, $workfifo)});
my $thread_liberty = threads->create(sub {Plugins::Fifo->run($conf, $products, $workliberty)});

接着 :$thread_fifo->join(); $thread_liberty->join();

这是错误消息:

Thread 1 terminated abnormally: Can't call method "getChildrenByTagNameNS" on unblessed reference at C:/strawberry/perl/site/lib/XML/Atom/Util.pm line 61.

要查看$thread_fifo我使用的是什么 ref 和 Dumper :

print ref($thread_fifo); # output : threads

print Dumper($thread_fifo); #output : $VAR1 = bless( do{\(my $o = '78589096')}, 'threads' );

我知道一个未受祝福的引用错误是一个变量不是对一个对象的合法引用,但是却试图在其上调用一个函数,就好像它是一个合法对象一样,但是我看不出问题出在哪里,我只是我想做的是同时调用两个函数。

提前致谢。

4

1 回答 1

2

不是一个完整的解决方案,但应该足以看到发生了什么

threads->create(\&foobar,$products,$workfifo,'info');
threads->create(\&foobar,$products,$workliberty,'liberty');

# Master Thread
my @threads = threads->list();
for(my $i=0; $i<scalar(@threads); ++$i) {
  print STDERR "MASTER: about to join thread $i\n";
  my $thread = $threads[$i];
  eval {
    $thread->join();
  };
  if($@) {
    print STDERR "Caught error while joining thread $i ($@)\n";
  }
  else {
    print STDERR "MASTER: finished joining thread $i\n";
  }
}
@threads = threads->list();
print STDERR "I GOT " . scalar(@threads) . ", NOW EXITING\n";
exit;

# Child threads
sub foobar {
  my ($products,$work,$str) = @_;
  print STDERR "CHILD $str: STARTING\n";
  Plugins::Fifo->run($conf, $products, $work);
  print STDERR "CHILD $str: ENDING\n";
}
于 2013-10-08T12:44:57.610 回答