Multithreading 带有线程的Perl脚本以渐进方式而非异步方式工作

Multithreading 带有线程的Perl脚本以渐进方式而非异步方式工作,multithreading,perl,Multithreading,Perl,我正在使用Perl5.10.1中的线程进行一些测试,但是我遇到了一些问题。首先,我有2.6.32-5-amd64(64位)的Debian GNU/Linux压缩/sid 这是我的剧本 #!/usr/bin/perl -w use strict; use warnings; use threads; sub threadProcess{ my ($number, $counter) = @_; print "Enter thread #" . $number . "\n";

我正在使用Perl5.10.1中的线程进行一些测试,但是我遇到了一些问题。首先,我有2.6.32-5-amd64(64位)的Debian GNU/Linux压缩/sid

这是我的剧本

#!/usr/bin/perl -w
use strict;
use warnings;
use threads;  

sub threadProcess{
    my ($number, $counter) = @_; 
    print "Enter thread #" . $number . "\n";
    while($counter < 10){
        print "Thread #" . $number . ": " . $counter . "\n";
        $counter++;
    }
    print "Exit thread #" . $number . "\n";
}

sub main{ 
    my $counter = 0;

    my $thr1 = threads->create(\&threadProcess, 1, $counter);  
    my $thr2 = threads->create(\&threadProcess, 2, $counter); 

    my $res1 = $thr1->join();  
    my $res2 = $thr2->join(); 

    print "Bye...\n";
}

main(@ARGV);

有什么问题吗?提前谢谢

什么都没有,除了
threadProcess
的作业很短,第一个线程可以在第二个线程初始化之前完成

在循环中放置一个延迟,您将看到线程同时工作

while($counter < 10){
    print "Thread #" . $number . ": " . $counter . "\n";
    sleep 1;      # or Time::HiRes::sleep 0.25, etc.
    $counter++;
}
while($counter<10){
打印“线程”#“$number.”:“$counter.”\n”;
睡眠1;#或时间::雇佣::睡眠0.25,等等。
$counter++;
}

什么都没有,除了
threadProcess
的作业很短,第一个线程可以在第二个线程初始化之前完成

在循环中放置一个延迟,您将看到线程同时工作

while($counter < 10){
    print "Thread #" . $number . ": " . $counter . "\n";
    sleep 1;      # or Time::HiRes::sleep 0.25, etc.
    $counter++;
}
while($counter<10){
打印“线程”#“$number.”:“$counter.”\n”;
睡眠1;#或时间::雇佣::睡眠0.25,等等。
$counter++;
}

谢谢哈哈哈,我错过了那部分,我的错,但是谢谢!!谢谢哈哈哈我错过了那一部分,我的坏,但是谢谢!!