in reply to Printing to STDERR causes deadlocks.
Below is a version of the code in the OP that (on my system) works reliably and apparently quite efficiently regardless of tracing, filesize, system load, whatever.
I would be most grateful if people running a multithreaded perl on non-Win platforms, and especially anyone with a multi-cpu machine, could try this code and report back their findings.
#!/usr/bin/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-Waiting' . $/; select undef,undef, undef, 0.0001 while $$sharedDataRef; warn "t-Locking" . $/; lock $$sharedDataRef; warn 't-Setting' . $/; $$sharedDataRef = $handle->getline; } $$doneRef = 1; return; } my $handle = IO::File->new( $ARGV[ 0 ], 'r' ); my $sharedData :shared = undef; my $done :shared = 0; threads->create( \&getDataT, $handle, \$sharedData, \$done ); while( !$done ) { warn 'm-waiting' . $/; select undef, undef, undef, 0.0001 until $sharedData; warn 'm-locking' . $/; lock $sharedData; warn 'm-Copying' . $/; my $localCopy = $sharedData; warn 'm-undefing' . $/; undef( $sharedData ); warn 'm-processing' . $/; processData( $localCopy ); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Cross-platform testers please.
by ghenry (Vicar) on Apr 27, 2005 at 10:15 UTC | |
by BrowserUk (Patriarch) on Apr 27, 2005 at 10:20 UTC | |
by ghenry (Vicar) on Apr 27, 2005 at 10:23 UTC | |
by BrowserUk (Patriarch) on Apr 27, 2005 at 10:52 UTC | |
by ghenry (Vicar) on Apr 27, 2005 at 10:57 UTC | |
|