in reply to Trouble implementing threads

Here is your basic thread joining loop. Remove your join from your creation sub, and put it in a waiting sub at the end of your main script.
for($ii=0;$ii<=$count;$ii++) { $thr="thread".$ii; print $thr,"\n"; # $$thr->join(); # don't join here } make a loop that waits for each thread as they finish # JOIN ALL THREADS # untested, but close :-) my @returns; while(1) foreach my $thread (threads->list) { if ($thread->is_joinable() ) { push @returns, $thread->join; } if( scalar (threads->list) == 0 ) { last} } print "@returns\n"; __END__

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Trouble implementing threads
by saranrsm (Acolyte) on Oct 21, 2011 at 10:11 UTC

    Dear monks I have modified as per zentara's advice but now it goes into a endless loop and get's killed ultimately. can some one help me in this.

    use threads ; use feature "say"; #use strict; use warnings; `tr -d '\n' </home/guru/Desktop/sequence.fasta >modified_human_chr_1.t +xt`; my $input_file = "modified_human_chr_1.txt"; open my $hd, "<", $input_file or die "Couldn't open '$input_file' - $! +"; my $text=<$hd>; chomp($text); my $window=5; my $i=0; LOOP:seek $hd,$i,0; read ($hd,my $substring,$window); my %hash; if(length($substring)==$window) { $hash{$substring}=$i; $i++; goto LOOP; } my $count = keys %hash; my $j=0; foreach my $pattern (keys %hash) { if($j<=$count) { my $thr="thread".$j; $$thr=threads->create(\&hunter,$text,$pattern); $j++; } } my @returns; foreach $thread (threads->list) { if ($thread->is_joinable() ) { push @returns, $thread->join; } if( scalar (threads->list) == 0 ) {last;} } print "@returns\n"; sub hunter { my ($text,$pattern) = @_ ; my $offset = 0; print "\n$pattern ($window)\n"; print '~' x $window,"\n"; my $pos=index $text,$pattern,$offset; while ($pos != -1) { print $pos+1," to ",$pos+$window,"\n"; $offset = $pos + 99; $pos = index($text, $pattern, $offset); } }
      To be honest, I can't understand what you are doing in your code, with the goto LOOP and the seek. Your first step should be to see how many threads are being created, you may have a thread bomb in your code. How many threads are reportedly created by the printout of $j, and how much memory are you consuming. Try watching the script with top, and see how much memory you use.

      You should strip down your code, to the point where you are just creating and joining threads which do nothing. When you get that to work, start adding your math routines, and see where it starts to break down.


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh