Dear Monks, I need to parallelize the process of finding positions of various patterns which has been collected and stored into a hash (refer the script which i have attached). if I dont parallelized the process of find patterns stored in a hash then all patterns has to stay in the queue until the preceding pattern is completed. So here I dont know whether to use threads or forks for achieving parallelization.

I have attached my script, where I have tried to implement perl ithreads to accomplish the parallelization (at line 25, inside the foreach loop). I dont think that the threads I have implemented works properly, because there's no visible improvement in the script as used without threads.

here in this paragraph, i have tried to explain the objective of my work. i get in a string and break it down into a length specified substrings (which may vary) and find the positions of those substrings.

use threads ; use feature "say"; use warnings; my $input_file = "text"; #Contains a single lined strin +g without any newline character open my $hd, "<", $input_file or die "Couldn't open '$input_file' - $! +"; my $text=<$hd>; chomp($text); my $window=5; #Length of the window my $i=0; LOOP:seek $hd,$i,0; #Here seek and read give us the + first $window lengthed substing runned over loop read ($hd,my $substring,$window); my %hash; if(length($substring)==$window) { $hash{$substring}=$i; #$substring pushed into a has +h, here I have avoided array has there may be repeated substrings in +the $text $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(\&position,$text,$pattern); $j++; } } for($ii=0;$ii<=$count;$ii++) { $thr="thread".$ii; print $thr,"\n"; $$thr->join(); } sub position { 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); } }

In reply to Trouble implementing threads by saranrsm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.