简体   繁体   English

如果启动多个线程,Perl的线程连接方法是否会等待第一个线程完成?

[英]Does Perl's thread join method wait for the first thread to finish if I launch more than one thread?

     foreach $name (@project_list)
     {
               # a thread is created for each project
               my $t = threads->new(\&do_work, $name);
               push(@threads, $t);
     } 

    foreach (@threads) {
     my $thrd = $_->join;
     print "Thread $thrd done\n";
    }

    sub do_work {
     # execute some commands here...
    }

The project_list is a list of 40 items. project_list是40个项目的列表。 When I spawn a thread for each item, will the join method wait for the first thread to finish and then move over to the next one and so on? 当我为每个项目生成线程时,join方法会等待第一个线程完成,然后移至下一个线程,依此类推吗?

If that is the case, then is it possible to avoid it? 如果是这样,有可能避免吗? I mean some threads will finish faster then others so why wait? 我的意思是有些线程会比其他线程更快地完成,所以为什么要等待?

Please let me know if more information is required. 如果需要更多信息,请告诉我。 Thank you. 谢谢。

$_->join waits for the thread designated by $_ to finish. $_->join等待$_指定的线程完成。 Since you're pushing them in order, and foreach traverses the list in order, yes, you'll wait first for the first thread. 因为按顺序推送它们,并且foreach按顺序遍历列表,所以可以,您首先要等待第一个线程。

But that doesn't matter since you're waiting for all threads to finish. 但这无关紧要,因为您正在等待所有线程完成。 It doesn't matter if you wait for the fastest finishers or the slowest ones first - you'll be waiting for everyone anyway. 无论您是先等待最快的完成者还是最慢的完成者都没关系-无论如何,您将一直在等待所有人。

Why wait? 干嘛要等? It depends on the scope of the post-processing step. 这取决于后处理步骤的范围。

If the post-processing step needs all threads to finish before beginning work, then the wait for individual threads is unavoidable. 如果后处理步骤需要所有线程在开始工作之前完成,那么不可避免地要等待各个线程。

If the post-processing step is specific to the results of each thread, it should be possible to make the post-processing part of the thread itself. 如果后处理步骤特定于每个线程的结果,则应该可以使线程本身成为后处理的一部分。

In both cases, $_->join foreach @threads; 在两种情况下, $_->join foreach @threads; is the way to go. 是要走的路。


If there is no need to wait for the threads to finish, use the detach command instead of join . 如果不需要等待线程完成,请使用detach命令而不是join However, any results that the threads may return will be discarded. 但是,线程可能返回的任何结果都将被丢弃。

The main thread has to live for the duration of all threads so you need to know when they are all done. 主线程必须存在于所有线程的持续时间内,因此您需要知道它们何时完成。 The can be done queues or semaphores. 可以完成队列或信号量。 Semaphores are the simplest: 信号量是最简单的:

use Thread::Semaphore;

my $S = Thread::Semaphore->new();


foreach $name (@project_list)
{
    # a thread is created for each project
    my $t = threads->new(\&do_work, $name);
    $S->down_force();                        # take one for each thread
} 

$S->down();                # this blocks until worker threads release one each
print "Thread $thrd done\n";


sub do_work {
    # execute some commands here...
    $S->up();             # here the worker gives one back when done.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM