in reply to Re^5: possibility to overcome perl threads
in thread possibility to overcome perl threads

Thanks. But it seems Thread::XXX are for 5005 thread. Does it work well with threads?
  • Comment on Re^6: possibility to overcome perl threads

Replies are listed 'Best First'.
Re^7: possibility to overcome perl threads
by BrowserUk (Patriarch) on Oct 31, 2009 at 01:37 UTC

    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.