Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to not wait for a thread

by renodino (Curate)
on Oct 25, 2007 at 19:33 UTC ( #647255=note: print w/ replies, xml ) Need Help??


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


Comment on Re: How to not wait for a thread
Download Code
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://647255]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-08-23 20:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (178 votes), past polls