http://www.perlmonks.org?node_id=647255


in reply to How to not wait for a thread

Perl/Tk is not threads-friendly. Its not exactly threads-hostile, but you need to make sure you run Perl/Tk in its own thread, and don't spawn any new threads from the same thread as Perl/Tk runs in.

That means you can't just rely on MainLoop magically stepping aside to check on your other threads, nor can you fiddle with Tk's structures from an external thread. You'll need to add a repeat timer event before calling MainLoop, and use queues to pass commands/data back and forth.

Here's some (untested) code snippets to get you going.

#!/usr/bin/perl -w use threads; use Thread::Queue; use Tk; use strict; my $cmdq = Thread::Queue->new(); my $respq = Thread::Queue->new(); my $thrd = threads->create(\&image_loader); my $mw = MainWindow->new; # ...the usual widget building here... my $repeat_id = $mw->repeat($interval, \&image_handler); $cmdq->enqueue('GO'); MainLoop; sub image_handler { return unless $respq->pending; my $buffer = $respq->dequeue(); ...load into your frame here... $cmdq->enqueue('GO'); } # # assumes your image loader persists # sub image_loader { while (1) { my $go = $cmdq->dequeue(); last if ($go eq 'STOP'); ...do your image loading business here... $respq->enqueue($imagebuffers); } }

Perl Contrarian & SQL fanboy

Replies are listed 'Best First'.
Re^2: How to not wait for a thread
by Sixtease (Friar) on Oct 25, 2007 at 20:03 UTC

    OK thanks a lot guys.

    So I need to use active waiting (timer). I was hoping for a passive waiting method (get a signal or sth) but this seems OK. I'll do as you advise. Thank you once again.