<DIV>Cara,</DIV>
<DIV>Você vai ter que criar o mecanismo de controle da quantidade de threads está sendo executada, eu recomendo você dar uma olhada no link <A href="http://migo.sixbit.org/papers/Perl_Threads/slide-index.html">http://migo.sixbit.org/papers/Perl_Threads/slide-index.html</A> para ter uma boa visão sobre threads e <A href="http://search.cpan.org/search?query=Threads&mode=all">http://search.cpan.org/search?query=Threads&mode=all</A> para as ferramentas de threads.</DIV>
<DIV> </DIV>
<DIV>Estou enviando os código abaixos para vc ver como poderia ser 'controlado' estas threads.</DIV>
<DIV> </DIV>
<DIV><code><BR>use threads;<BR>use threads::shared;</DIV>
<DIV>our $TCounter : shared;</DIV>
<DIV>$MaxThreads = 25;</DIV>
<DIV>while ( loop de alguma coisa ) {<BR> lock ( $TCounter ); <BR> $TCounter++;<BR> my $Thread = threads->create(\&Algumacoisa, $Valor01, $Valor02 );<BR> $Thread->detach();<BR> <BR> while ( $TCounter >= $MaxThreads ) {<BR> print "Aguando liberar threads : $TCounter \n";<BR> threads->yield();<BR> sleep 1;<BR> }<BR>}</DIV>
<DIV>while ( $TCounter )<BR>{<BR> print "\tAguando fim de threads\n";<BR> sleep 10;<BR>}</DIV>
<DIV>sub Algumacoisa {<BR> my ($valor01, $valor02) = @_;<BR># Faça as coisas..<BR># e ao terminar ...<BR>lock ( $TCounter );<BR>$TCounter--;<BR>}<BR></code></DIV>
<DIV>Bom, nesta primeira você viu que o elo fraco está no método 'Algumacoisa', pois se vc sair dele sem diminuir a variável $TCounter, o sistema pode entrar num loop infinito. Para contonar o o problema vc pode colocar timeout no loop.</DIV>
<DIV> </DIV>
<DIV>Uma outra alternativa pode ser algo assim :<BR><code><BR>use Thread::Running qw();<BR>use threads;</DIV>
<DIV>$MaxThreads = 25;</DIV>
<DIV>for ( 1..100 ) {<BR> my $Thread = threads->create(\&Algumacoisa, $_ );<BR> my @Contador = threads->list;<BR> <BR> while ( $#Contador >= $MaxThreads ) {<BR> print "Aguardando liberar as threads $#Contador\n";<BR> foreach ( threads->list ) {<BR> if (! $_->running ) {<BR> print "\t", $_->tid, " is not running\n";<BR> $_->tojoin;<BR> pop @Contador;<BR> }<BR> else {<BR> print "\t", $_->tid, " is running\n";<BR> }<BR> }<BR> threads->yield();<BR> sleep 1;<BR> }<BR>}</DIV>
<DIV>print "Indo para o final !!\n";</DIV>
<DIV>foreach ( threads->list ) {<BR> print "Aguardando thread numero ", $_->tid, "\n";<BR> $_->join;<BR>}</DIV>
<DIV>sub Algumacoisa {<BR> my ( $valor02 ) = shift;<BR> # Faça as coisas..<BR> # e ao terminar ...<BR> print "Thread de numero $valor02\n";<BR> sleep 10;<BR> print "Finalizando a thread de numero $valor02\n";<BR>}<BR></code></DIV>
<DIV><BR> </DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#f0f0f0 border=0>
<TBODY>
<TR>
<TD width=70 bgColor=#bde9fd><FONT face="Verdana,Arial,'Trebuchet MS'" size=2><B>De:</B></FONT></TD>
<TD><FONT face="Verdana,Arial,'Trebuchet MS'" size=2>cascavel-pm-bounces@pm.org</FONT></TD></TR></TBODY></TABLE></DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#f0f0f0 border=0>
<TBODY>
<TR>
<TD width=70 bgColor=#bde9fd><FONT face="Verdana,Arial,'Trebuchet MS'" size=2><B>Para:</B></FONT></TD>
<TD><FONT face="Verdana,Arial,'Trebuchet MS'" size=2>cascavel-pm@pm.org</FONT></TD></TR></TBODY></TABLE></DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#f0f0f0 border=0>
<TBODY>
<TR>
<TD width=70 bgColor=#bde9fd><FONT face="Verdana,Arial,'Trebuchet MS'" size=2><B>Cópia:</B></FONT></TD>
<TD><FONT face="Verdana,Arial,'Trebuchet MS'" size=2></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#f0f0f0 border=0>
<TBODY>
<TR>
<TD width=70 bgColor=#bde9fd><FONT face="Verdana,Arial,'Trebuchet MS'" size=2><B>Data:</B></FONT></TD>
<TD><FONT face="Verdana,Arial,'Trebuchet MS'" size=2>Wed, 31 Aug 2005 00:15:50 -0300</FONT></TD></TR></TBODY></TABLE></DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#f0f0f0 border=0>
<TBODY>
<TR>
<TD width=70 bgColor=#bde9fd><FONT face="Verdana,Arial,'Trebuchet MS'" size=2><B>Assunto:</B></FONT></TD>
<TD><FONT face="Verdana,Arial,'Trebuchet MS'" size=2>[Cascavel-pm] Threads</FONT></TD></TR></TBODY></TABLE></DIV>
<DIV>> olá pessoal</DIV>
<DIV>> </DIV>
<DIV>> cá estou eu mais uma vez pedindo a ajuda para um de vocês.</DIV>
<DIV>> </DIV>
<DIV>> </DIV>
<DIV>> preciso saber o seguinte:</DIV>
<DIV>> </DIV>
<DIV>> como controlar o número de threads ativos?</DIV>
<DIV>> ou seja digamos que meu programa tenha o limite de 25 threads</DIV>
<DIV>> e assim que um thread fechar eu abra outro ficando sempre com 25 ativos</DIV>
<DIV>> alguem sabe me explicar isso ?</DIV>
<DIV>> </DIV>
<DIV>> modulos usados:</DIV>
<DIV>> </DIV>
<DIV>> use Thread;</DIV>
<DIV>> use threads;</DIV>
<DIV>> </DIV>
<DIV>> obrigado. </DIV>
<DIV>> </DIV>
<DIV>> </DIV>
<DIV>> _______________________________________________</DIV>
<DIV>> Cascavel-pm mailing list</DIV>
<DIV>> Cascavel-pm@pm.org</DIV>
<DIV>> http://mail.pm.org/mailman/listinfo/cascavel-pm</DIV>