[SP-pm] threads

Jorge Teles jorteles em yahoo.com
Sexta Maio 5 11:17:35 PDT 2006


Ok! Iniciei a tradução, dentro de alguns dias envio
para a lista para aqueles que quiserem fazer a
revisão.

Até....

--- Solli Honorio <shonorio em gmail.com> wrote:

> On 5/3/06, Jorge Teles <jorteles em yahoo.com> wrote:
> >
> > Ol� pessoal,
> >
> > Estou comen�ando uns estudos em threads em perl:
> >
>
http://migo.sixbit.org/papers/Perl_Threads/slide-index.html
> >
> > ..... code ....
> >
> > Nesta p�gina tem um exemplo de c�lculo de
n�meros
> > primos, mas h� uma coisa que n�o entendi no
> exemplo,
> > talvez porque esteja faltando algum conceito.
> > Eu n�o entendi muito bem o que acontece na
fun�ao,
> na
> > parte em que ele inicializa novamente a thread com
> a
> > mesma vari�vel, algu�m poderia dar um help.
> >
> > At�...
> 
> 
> 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 perlthrtut - tutorial on
threads
> in
>
Perl<http://www.perl.org.br/view/Perldoc/Perlthrtut>.
> Alias, j� que o teu interesse neste assunto �
> grande, ler e traduzir este
> tutorial pode ser muito interessante.
> 
> Abaixo mostra como o c�digo funciona :
> 
> ========================
> 
> 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.
> 
> 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)
> 
> The bulk of the work is done by the check_num()
> 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).
> 
> 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.
> 
> 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.
> 
> 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).
> 
> 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.
> 
> That's how it works. It's pretty simple; as with
> many Perl programs, the
> explanation is much longer than the program.
> > _______________________________________________
> SaoPaulo-pm mailing list
> SaoPaulo-pm em pm.org
> http://mail.pm.org/mailman/listinfo/saopaulo-pm




__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


Mais detalhes sobre a lista de discussão SaoPaulo-pm