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

Actually, Tk objects. I want to change the appearance of UI while executing something on a worker thread...
  • Comment on Re^4: possibility to overcome perl threads

Replies are listed 'Best First'.
Re^5: possibility to overcome perl threads
by BrowserUk (Patriarch) on Oct 30, 2009 at 16:44 UTC

    Then you asked the wrong question.

    Even if there were an alternative implemetation of threading to threads, it wouldn't help because Tk objects are not thread-safe.

    Indeed. most guis use a queuing mechanism to allow background threads to interact with the gui thread (singular), and that is an efficient and relatively easy thing to do using threads and Thread::Queue.


    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.
      Thanks. But it seems Thread::XXX are for 5005 thread. Does it work well with threads?

        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.