gants has asked for the wisdom of the Perl Monks concerning the following question:

hi every1, I write this snippet so as to do priority for everyone queue, but what's wrong with join? Actually in POSIX C-lang I use pthread_join, I've read docs about perl thread, but still complicated why happening so funky things with that? Also for input values into testing log I do echo 4-2-67-1 >> tst.log.
use threads; my %queue; file_watchdog(); exit 1; sub file_watchdog { open(TRAP, '<tst.log') or die "can't open file_watchdog!"; while (1) { select(undef,undef,undef,0.2); my $a .= <TRAP>; if(length($a) gt 0 ){ checker($a); } } close (TRAP); } sub checker { my ($y) = @_; if($y =~ /(\d+)\-(\d+)\-(\d+)\-(\d+)/){ my $ip =$1; my $idx= $2; my $time = $3; my $th = $4; my $thr_h; $queue{$ip}{$idx}{TICK} = $time; if($time < 5 ){ my $wait = 7-$time; $queue{$ip}{$idx}{WAIT} = $wait; print "IP:$ip IDX:$idx TIME:$time TH:$th WAIT:$wait \n"; if($th){ #Thread 2 terminated abnormally: Not a CODE reference + at thread.pl line 34, <TRAP> line 2. $thr_h->{$ip} = threads->create(\&wait_q($ip,$idx)); #->join() +; }else{ wait_q($ip,$idx); } }else{ if ($thr_h->{$ip}->is_joinable()) {#Can't call method "is_join +able" on an undefined value at thread.pl line 40, <TRAP> line 3. $thr_h->{$ip}->join(); } print "do_connection() IP:$ip\n"; } } } sub wait_q { my $ip = shift; my $idx = shift; sleep( $queue{$ip}{$idx}{WAIT} ); oid_checker($ip); }

Replies are listed 'Best First'.
Re: threads join()
by zentara (Cardinal) on Oct 30, 2009 at 11:38 UTC
    my first GUESS ... your code starts it's thread as part of a sub, in a dynamic manner, with all sorts of time delays involved

    .... the way a thread written in c gets joined, and the way the Perl interpreter does it's joining and cleanup are 2 different ways of behavior

    In c, if you tell it to join the thread ...it will lop it's head off right there, so you may be lucking out and it is running (albeit luckily)

    In Perl, thread joining and cleanup hasn't been that good,

    the difference is that funky behavior, of which you complain.. :-)

    you can search the old nodes for topics like "perl reusable threads", and see why in Perl, you are often better off pre-creating permanent , reusable threads, and reuse them, instead of counting on Perl's join to clean house for you.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: threads join()
by llancet (Friar) on Oct 30, 2009 at 14:41 UTC
    Perl threads is something fake. All variables are made a copy for each thread by default. See threads::shared.