#! perl -slw use strict; use threads qw[ yield ]; use threads::shared; use Thread::Queue; my $Q = Thread::Queue->new; sub complexGenerator{ my( $max ) = @_; for( 1 .. $max ){ # Do our complex generation here # wait till they are ready for the next result select undef, undef, undef, 0.01 and yield while $Q->pending; # and let them have it $Q->enqueue( $_ ); } # Indicate we're done $Q->enqueue( undef ); } my $coro = threads->new( \&complexGenerator, 100 ); while( my $next = $Q->dequeue ) { # Process latest result set printf $next; ; } $coro->join; __END__