Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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 making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2014-09-22 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (206 votes), past polls