in reply to Thread in perl
When a thread updates a shared variable, the main thread will not see the update until it reads the shared variable again. A common way to do this is to have a timer in the Tk GUI to read the shared variable every 10 milliseconds( or whatever rate is good enough for you). You don't show any real code involving sockets, but Simple threaded chat server might help you with some ideas.
A simple example of a timer based method:
#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; # declare, share then assign my $ret; share $ret; $ret = 0; my $val = 0; #create thread before any tk code is called my $thr = threads->create( \&worker ); use Tk; my $mw = MainWindow->new(); my $label = $mw->Label( -width => 50, -textvariable => \$val )->pack(); my $timer = $mw->repeat(10,sub{ $val = $ret; }); MainLoop; # no Tk code in thread sub worker { for(1..10){ print "$_\n"; $ret = $_; sleep 1; } $ret = 'thread done, ready to join'; print "$ret\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Thread in perl
by priyaviswam (Sexton) on Sep 09, 2011 at 03:51 UTC |