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(); }