jasoncollins has asked for the wisdom of the Perl Monks concerning the following question:
Hi All, I have the following threaded code that works unreliably, it works more freuqntly than not, but when it doesn't work I hit a print statement below "The filename is: $fn" and then the script ends! Any ideas would be appreciated.
use threads; use threads::shared; use Thread::Semaphore; use IO::Handle; use strict; my $sem = Thread::Semaphore->new(); my $seq :shared = 1; my $th1 = threads->create('Printer', $ARGV[0]); my $th2 = threads->create('Printer', $ARGV[1]); $th1->join(); $th2->join(); sub Printer { my $f = new IO::Handle; my $fn = shift; my $ifh; print "The filename is: $fn\n"; if (open ($ifh, '<', $fn) == 0 ) { my $tid = threads->tid(); print "\n\nError: problem opening up file: $fn. Exiting threads: + $tid\n"; return 1; } my $msg = ""; if ($f->fdopen(fileno($ifh),"r")) { print "successfully opened up .\n"; } else { print "\n\nError opening up input file.\n"; return 2; } while (defined($msg = $f->getline())) { $sem->down(); $msg = "<$seq>: " . "$msg"; print "message is: $msg\n"; $seq++; $sem->up(); } $f->close(); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Crahes due to threads.
by BrowserUk (Patriarch) on Jul 23, 2010 at 02:16 UTC | |
by jasoncollins (Novice) on Jul 23, 2010 at 16:32 UTC | |
by BrowserUk (Patriarch) on Jul 23, 2010 at 16:43 UTC | |
|
Re: Crahes due to threads.
by ikegami (Patriarch) on Jul 22, 2010 at 23:56 UTC | |
by jasoncollins (Novice) on Jul 23, 2010 at 16:23 UTC | |
by jasoncollins (Novice) on Jul 23, 2010 at 16:26 UTC | |
|
Re: Crahes due to threads.
by cdarke (Prior) on Jul 23, 2010 at 10:22 UTC | |
by jasoncollins (Novice) on Jul 23, 2010 at 16:33 UTC |