Thanks. But it seems Thread::XXX are for 5005 thread. Does it work well with threads? | [reply] |
Yes. Thread::Queue works perfectly in conjunction with threads, despite that the naming convention suggests otherwise (something I've bemoaned myself on a few occasions). By way of demonstration, here's a somewhat trivial tk app that starts two worker threads each updating a different gui element via a single queue:
#!perl -slw
use strict;
use threads;
use Thread::Queue;
## A shared var to communicate progess between work thread and TK
my $Q = new Thread::Queue;
sub work{
my( $id, $delay ) = @_;
open PROC,
qq[ perl -le"\$|=1; print and select('','','', $delay ) for 1 .. 1
+00" |]
or die $!;
while( <PROC> ) {
$Q->enqueue( "$id:$_" );
}
close PROC;
}
threads->new( \&work, $_, 0.1 * $_ )->detach for 1 .. 2;
## For lowest memory consumption require (not use)
## Tk::* after you've started the work thread.
require Tk::ProgressBar;
my $mw = MainWindow->new;
my $pb1 = $mw->ProgressBar()->pack();
my $lb = $mw->Label( -height => 2 )->pack;
my $pb2 = $mw->ProgressBar()->pack();
my $repeat;
$repeat = $mw->repeat( 100 => sub {
while( $Q->pending ) {
my( $id, $progress ) = split ':', $Q->dequeue;
return unless $progress;
( $id == 1 ? $pb1 : $pb2 )->value( $progress );
if( $id == 2 && $progress == 100 ) {
$repeat->cancel;
$mw->exit;
}
}
} );
$mw->MainLoop;
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |