#! 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; } }