Rather than messing around with IO::String or in-memory files and then needing to use substr to try and extract the new bits of the log etc., I think I have an alternative that will achieve your aim.
The basic idea is to tie a filehandle to a Thread::Queue object, which can be shared across threads. You tie a filehandle and then pass this into each of your Telnet threads and they pass it to their respective Net::Telnet objects for logging.
Your main thread can retrieve the thread::Queue handle from the tie and the loop over reading from it and displaying what it gets into the appropriate window(s). It can also, output the lines to the permeanent log file(s) within the same loop.
This 'loop' could also be a Tk event callback.
I hope the following (crude) code will demonstrate the idea.
#! perl -slw use strict; use threads qw[ yield ]; use Thread::Queue; package Tie::Glob::Queue; sub TIEHANDLE { my $class = shift; my $self = new Thread::Queue; return bless \$self, $class; } sub PRINT { my $Q = ${ shift() }; $Q->enqueue( join( $, || '', @_ ) ); } 1; package main; our $TELNETS ||= 10; my $threads : shared = 0; sub telnets{ { lock $threads; $threads++ } my $io = shift; for( 1 .. 5 ) { print $io threads->tid(), " : $_"; select undef, undef, undef, rand 1; } { lock $threads; $threads--; } } my $Q = ${ tie *LOG, 'Tie::Glob::Queue' }; my @threads = map{ threads->create( \&telnets, \*LOG )->detach } 1 .. +$TELNETS; yield while $threads < $TELNETS; while( $threads ) { if( $Q->pending ) { print $Q->dequeue; } else{ yield; } }
The tie package will need filling out (probably by inheriting from Tie::StdHandle) and the telnets() sub is just a placeholder but the basic methodology is there.
In reply to Re^5: Problem with using threads with modules.
by BrowserUk
in thread Problem with using threads with modules.
by tele2mag
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |