The following program run as test somefile , will (on my system) reliably copy the contents of the named file to standard out and then terminate.
But if you run as test -TRACE=1 somefile it will almost always hang, deadlocked.
#! perl -slw use strict; use IO::File; use threads; use threads::shared; BEGIN { $| = 1; our $TRACE ||= 0; print "TRACE=$TRACE"; *CORE::GLOBAL::warn = sub {} unless $TRACE; } sub processData { printf @_; } sub getDataT { my ( $handle, $sharedDataRef, $doneRef ) = @_; while( !$handle->eof ) { warn "t-Locking" . $/; lock $$sharedDataRef; warn 't-Waiting' . $/; cond_wait( $$sharedDataRef ) while $$sharedDataRef; warn 't-Setting' . $/; $$sharedDataRef = $handle->getline; warn 't-Signalling' . $/; cond_signal( $$sharedDataRef ); } $$doneRef = 1; return; } my $handle = IO::File->new( $ARGV[ 0 ], 'r' ); my $sharedData :shared; my $done :shared = 0; threads->create( \&getDataT, $handle, \$sharedData, \$done ); while( !$done ) { warn 'm-locking' . $/; lock $sharedData; warn 'm-waiting' . $/; cond_wait $sharedData until $sharedData; warn 'm-Copying' . $/; my $localCopy = $sharedData; warn 'm-undefing' . $/; undef( $sharedData ); warn 'm-signalling' . $/; cond_signal $sharedData; warn 'm-processing' . $/; processData( $localCopy ); }
All the -TRACE=1 option does is cause it to print some debug lines to STDERR.
3 questions:
I'd be particularly interested in hearing about it's behaviour on multi-cpu systems.
Many thanks.
In reply to Printing to STDERR causes deadlocks. by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |