Oh sagacious and munificent monks;
In my eternal attempt to better myself, I've been trying to learn threads under Perl. I wrote the following code to play with concepts in message dispatching. Ideally, it should just echo input back at me until I 'quit'. It behaves nicely on my Linux box (5.8.8 64-bit multithread under Debian) but hangs on my Windows box (ActiveState perl v5.8.8 MSWin32-x86-multi-thread on a dual-core) on line 26 on the first invocation. Any ideas on routes I could pursue to fix this?
#!/usr/bin/perl use strict; use warnings; use threads; use Thread::Queue; # Create terminal watcher print "Create terminal watcher...\n"; my $Q_stdin = Thread::Queue->new; async { $Q_stdin->enqueue( $_ ) while defined( $_ = <STDIN> ); }->detach; my $Q_found = Thread::Queue->new; my $cmd; print "Awaiting commands...\n"; MAIN_LOOP: while (not defined $cmd or $cmd !~ /^q/i) { sleep(1); # Reduce load # Process commands $cmd = $Q_stdin->dequeue_nb; if (defined $cmd) { chomp $cmd; if ($cmd =~ /^q/i) { print "Resolving open threads\n"; } else { async { $Q_found->enqueue( $cmd ) ; }->detach; #$Q_found->enqueue( $cmd ) ; # using this in place of asyn +c works } } # Print announcements while (defined(my $output = $Q_found->dequeue_nb)) { print $output, "\n"; } }
Update: The issue can be resolved by ensuring STDIN is closed before entering MAIN_LOOP (thanks oshalla). The reason for it is still not entirely clear in my mind, but I have some clear avenues of research to pursue - there seem to be both OS and Perl version variations impacting the behaviors of this code. In any case, a more verbose version that behaves as expected follows:
#!/usr/bin/perl use strict; use warnings; use threads; use Thread::Queue; use Thread::Semaphore; # Create terminal watcher print "Create terminal watcher...\n"; my $Q_stdin = Thread::Queue->new ; my $S_stdin = Thread::Semaphore->new(0) ; print "fileno(STDIN) = ", fileno(STDIN), "\n" ; async { $S_stdin->down() ; print "Reading STDIN, fileno=", fileno(STDIN), "\n" ; while (defined( $_ = <STDIN> )) { chomp ; print "\n+IN : '$_'\n" ; $Q_stdin->enqueue( $_ ) ; }; }->detach; close STDIN ; ### no longer our business + <<<< print "STDIN is: ", defined(fileno(STDIN)) ? 'open' : 'closed', "\n" ; $S_stdin->up() ; my $Q_found = Thread::Queue->new; my $cmd; my $quit_flag = 1; print "Awaiting commands...\n"; MAIN_LOOP: while ($quit_flag) { sleep(1); # Reduce load # Process commands while (defined ($cmd = $Q_stdin->dequeue_nb)) { chomp $cmd; if ($cmd =~ /^q/i) { print "Resolving open threads\n"; $quit_flag = 0; } else { async { $Q_found->enqueue( $cmd ) ; }->detach; #$Q_found->enqueue( $cmd ) ; # using this in place of asyn +c works } } # Print announcements while (defined(my $output = $Q_found->dequeue_nb)) { print "+OUT: '$output'\n"; } }
In reply to threads on Windows by kennethk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |