Thread

Posted on Tue Jun 19 21:12:01 2007 by calli
pls a little help, kind of howto..
Hi,
I have made a little program using an older version of threads and now it does not work properly.. So my little question what is wrong now? 1st thread is listening to my tcp-traffice (using NetPacket) $q = ".."; and pushes this a) { lock($PIPS); # only for 2nd thread!! $PIPS .= $q; # } and pushes it to a threaded chat-Array to send to any socket client that has been accepted: { lock(@chat); # push for sending to all push @chat, $qtsID."\t".$q; # $qtsID is an identifier not to send back to origine cond_signal(@chat); } 2nd thread takes this to do s.th with it: { lock($PIPS); $nPips = $PIPS; $PIPS = ''; } ... and pushes the result as well to the chat-Array.. { lock(@chat); push @chat; cond_signal(@chat); } # un-block vars block Finally pairs of threads were created when aksed for a socket-connection. The 'Reader' (here) just listens to know when to die: while(defined ($l = <$socket>) ){ # empty loop } { lock($NoClient); $NoClient--; } # if you want to kill the Writer immediately and not only after the next input: de-commet: { lock(@chat); cond_broadcast(@chat); } and the 'Sender' after sending some files send those performs: while( 1 ) { lock(@chat); cond_wait(@chat);# if (@chat); next unless (@chat); ... foreach (@chat) { ... print $SOCKET ... } } Well this has worked, but now - I think - it blocks everything right after the first client has died... Do I just have to put bracktes within while(1) ? while( 1 ) { {# add bracket open lock(@chat); cond_wait(@chat); next unless (@chat); ... foreach (@chat) { ... print $SOCKET ... } } # add bracket close }
Thanks in advance, Carl
Direct Responses: 5476 | Write a response
Posted on Tue Jun 19 21:53:37 2007 by jdhedden in response to 5475
Re: pls a little help, kind of howto..
> Do I just have to put brackets within while(1)?

I don't think you need to. Loops are supposed to reinitialize the scope each time. However, you could try it to see if it fixes your problem.
Direct Responses: 5487 | Write a response
Posted on Wed Jun 20 19:01:37 2007 by calli in response to 5476
Re: pls a little help, kind of howto..
The problem occurs when a client drops its connection. Once the whole program stops absolutely nothing happend so it seems that both initial threads were blocked, another time only the first thread was blocked and the second thread one tries to make 'him' work again, but the second does not print out anything to the logfile ?? Could it be a problem that the Writer does this:
foreach my $f ( liesFiles($tmpMinDir) ) { next unless ( $f && (-s $f) ); print $SOC "# \tNow $thisPC will send you quotes of zip-file:$f, ".(-s $f)." bytes $EOL" +; open( SAVEOUT, ">&STDOUT" ) or warn "can't save stdout: $!\n"; open( STDOUT, ">&", $SOC ) or warn "can't dup to stdout: $!\n"; if ($f =~ /\.zip/) { system('nice','-1','unzip', '-p', $f ); } else { system('nice','-1','cat', $f); } open( STDOUT, ">&SAVEOUT" ) or warn "can't restore stdout: $!\n"; }
Do the threads have different STDIN and STDOUT? But the Writer is killed only after the cond_broadcast(@chat) of the Reader..? I derived my program, from a threaded-chat server, that I once programed and that you can
load from here: http://www.perlmonks.net/index.pl?node_id=473566. May be that even this wont work anymore propperly? Cheers,Carl
Direct Responses: 5490 | Write a response
Posted on Wed Jun 20 20:06:29 2007 by jdhedden in response to 5487
Re: pls a little help, kind of howto..
The manipulation of STDOUT that you show is okay as far as I can tell.

Beyond that, without the code, there is no way to debug your problem, and I'm not going to look at the original program if you've made changes to it that could be problematic.

Therefore, please send the code and instructions on how to use it to: jdhedden AT cpan DOT org
Write a response