in reply to Re^4: threads, file handles and wxPerl
in thread threads, file handles and wxPerl
Should I do that explicitly?
No. Implicit closing works fine. Where the issue arises is typified by this TCP server constuction:
sub clientHandler { my( $fileno ) = shift; open my $client, ">&=$fileno" or die $!; while( <$client> ) { ...; } } while( my $client = $svr->accept ) { threads->create( \&clientHandler, fileno( $client ) ); }
The problem is that will work much of the time, especially when testing under light loads, because the thread will succeed in running to the point when the client handle is duped, before the thread that spawned it reaches the end of the while loop and the original $client is auto-closed.
But then when the program is used under moderately heavy loads, the spawning thread gets another timeslice before the spawned thread has chance to perform the dup, and by the time it does, the handle has been auto-closed and there is nothing to dup.
It becomes necessary to add a semaphore to prevent the parent thread from reaching the end of the while loop before the child has duped the handle. Not especially difficult; but there are many ways to do it wrongly.
|
---|