#!/usr/bin/perl use strict; use warnings; use threads; use Thread::Queue; my $input_q = Thread::Queue -> new(); sub stdin_reader { while ( my $line = ) { if ( defined $input_q -> pending() ) { $input_q -> enqueue ( $line ); } else { last; } } } sub doing_stuff_thread { my $count = 0; while ( $count < 10 ) { my $item = $input_q -> dequeue_nb(); if ( defined $item ) { print "Got: $item"; $count++; } else { print "Doing something else, because nothing on STDIN\n"; print "Pending items:", $input_q -> pending(), "\n"; sleep 1; } } $input_q -> end(); } threads -> create ( \&stdin_reader ); threads -> create ( \&doing_stuff_thread ); foreach my $thr ( threads -> list() ) { print "Waiting for ",$thr -> tid()," to join\n"; $thr -> join(); }