<br><br><div><span class="gmail_quote">On 5/3/06, <b class="gmail_sendername">Jorge Teles</b> &lt;<a href="mailto:jorteles@yahoo.com">jorteles@yahoo.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Olá pessoal,<br><br>Estou començando uns estudos em threads em perl:<br><a href="http://migo.sixbit.org/papers/Perl_Threads/slide-index.html">http://migo.sixbit.org/papers/Perl_Threads/slide-index.html</a><br><br>..... code .... 
<br><br>Nesta página tem um exemplo de cálculo de números<br>primos, mas há uma coisa que não entendi no exemplo,<br>talvez porque esteja faltando algum conceito.<br>Eu não entendi muito bem o que acontece na funçao, na<br>
parte em que ele inicializa novamente a thread com a<br>mesma variável, alguém poderia dar um help.<br><br>Até...</blockquote><div><br><span style="font-family: courier new,monospace;">Jorge, não sei se você tem algum problema com a leita/interpretação de texto em inglês (eu acho que não), por isto estou enviando o comentário sobre este código existente no 
</span><a href="http://www.perl.org.br/view/Perldoc/Perlthrtut"><font style="font-family: courier new,monospace;" size="2">perlthrtut - tutorial on threads in Perl</font></a><span style="font-family: courier new,monospace;">
. Alias, já que o teu interesse neste assunto é grande, ler e traduzir este tutorial pode ser muito interessante.<br><br>Abaixo mostra como o código funciona : <br><br>========================<br style="font-family: courier new,monospace;">
</span><p style="font-family: courier new,monospace;">This program uses the pipeline model to generate prime numbers.  Each
thread in the pipeline has an input queue that feeds numbers to be
checked, a prime number that it's responsible for, and an output queue
into which it funnels numbers that have failed the check.  If the thread
has a number that's failed its check and there's no child thread, then
the thread must have found a new prime number.  In that case, a new
child thread is created for that prime and stuck on the end of the
pipeline.</p>
<p style="font-family: courier new,monospace;">This probably sounds a bit more confusing than it really is, so let's
go through this program piece by piece and see what it does.  (For
those of you who might be trying to remember exactly what a prime
number is, it's a number that's only evenly divisible by itself and 1)</p>
<p style="font-family: courier new,monospace;">The bulk of the work is done by the <code>check_num()</code> subroutine, which
takes a reference to its input queue and a prime number that it's
responsible for.  After pulling in the input queue and the prime that
the subroutine's checking (line 20), we create a new queue (line 22)
and reserve a scalar for the thread that we're likely to create later
(line 21).</p>
<p style="font-family: courier new,monospace;">The while loop from lines 23 to line 31 grabs a scalar off the input
queue and checks against the prime this thread is responsible
for.  Line 24 checks to see if there's a remainder when we modulo the
number to be checked against our prime.  If there is one, the number
must not be evenly divisible by our prime, so we need to either pass
it on to the next thread if we've created one (line 26) or create a
new thread if we haven't.</p>
<p style="font-family: courier new,monospace;">The new thread creation is line 29.  We pass on to it a reference to
the queue we've created, and the prime number we've found.</p>
<p style="font-family: courier new,monospace;">Finally, once the loop terminates (because we got a 0 or undef in the
queue, which serves as a note to die), we pass on the notice to our
child and wait for it to exit if we've created a child (lines 32 and
37).</p>
<p style="font-family: courier new,monospace;">Meanwhile, back in the main thread, we create a queue (line 9) and the
initial child thread (line 10), and pre-seed it with the first prime:
2.  Then we queue all the numbers from 3 to 1000 for checking (lines
12-14), then queue a die notice (line 16) and wait for the first child
thread to terminate (line 17).  Because a child won't die until its
child has died, we know that we're done once we return from the join.</p>
<p style="font-family: courier new,monospace;">That's how it works.  It's pretty simple; as with many Perl programs,
the explanation is much longer than the program.</p></div></div>