in reply to Using threads with Ping

join is a blocked call, so it will not return until the child thread is joined, i.e. returned.

foreach(@list){ $th = threads->create("ping","$_"); # $th->join; # new thread is NOT created until join # not what I want! }
This does not work, because it will only go to the next iteration, i.e. create next child thread, when the previous one returned.

What you can do is to store all the child thread handlers in an array:
foreach(@list){ push @childs, threads->create("ping","$_"); } ... foreach $child (@childs){ $child->join(); }

Replies are listed 'Best First'.
Re: Re: Using threads with Ping
by JamesNC (Chaplain) on Jan 31, 2003 at 05:16 UTC
    Wow.. it works perfectly now :-) Thank you so very much! I see all the threads get created.. and now this time.. they join in different orders! Very fast too.. For anyone that interested, here is the working code with pg's fix:
    use threads; use threads::shared; use strict; my $wait = 500; my @up: shared; my $th; my @childs; my $child; my @down: shared; my @list = qw ( www.apple.com www.perl.org www.ibm.com www.coke.com www.oracle.com www.hp.com ); foreach(@list){ push @childs, threads->create("ping","$_"); } foreach $child (@childs){ $child->join(); } print @up; print @down; sub ping { my $ip = shift; my @rv = `ping -n 1 -w $wait $ip`; foreach(@rv){ push @up, "$ip up\n" if /\(0% loss\)/; push @down, "$ip down\n" if /\(100% loss\)/; } }