Home > Archive > PerlTk > June 2006 > Thread problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| limon7@gmail.com 2006-06-17, 8:14 am |
| I just write some code using Tk & Thread below. When I click the "plus"
Button, the woker thread increase $total, but the Label , in the Boss
thread, doesn't change with $total as I wish.
Then I have to click the "get_total" button to get the correct value of
$total for the Label, while I think it should change automaticly.
How to crrect it ?
use threads;
use Thread::Queue;
use threads::shared;
use Tk;
my $queue;
my $thr;
my $total:shared=0;
$queue = Thread::Queue->new;
my $queue = Thread::Queue->new;
$thr = threads->new(sub {
while(1)
{
$queue->dequeue;
lock($total);
print ++$total;
}
}
);
my $mw=new MainWindow;
my $btn=$mw->Button(-text=>'plus', -command=>sub {plus()})->pack();
my $btn2=$mw->Button(-text=>'get', -command=>sub
{get_total()})->pack();
my $lbl=$mw->Label(-textvariable=>\$total)->pack();
MainLoop;
sub plus()
{
$queue->enqueue(1);
}
sub get_total()
{
$lbl->configure(-text=>$total);
}
| |
| QoS@domain.invalid.com 2006-06-17, 8:14 am |
|
limon7@gmail.com wrote in message-id:
<1149777723.324065.311630@j55g2000cwa.googlegroups.com>
>
>I just write some code using Tk & Thread below. When I click the "plus"
>Button, the woker thread increase $total, but the Label , in the Boss
>thread, doesn't change with $total as I wish.
>Then I have to click the "get_total" button to get the correct value of
>$total for the Label, while I think it should change automaticly.
Perhaps it should, and not just that; the script functions differently
if $total is populated with text rather than numbers.
>How to crrect it ?
This works:
===========
use threads;
use Thread::Queue;
use threads::shared;
use Tk;
my $queue;
my $thr;
my $total:shared=0;
$queue = Thread::Queue->new;
my $queue = Thread::Queue->new;
$thr = threads->new(sub {
while(1)
{
$queue->dequeue;
lock($total);
print $total;
}
}
);
my $mw=new MainWindow;
my $btn=$mw->Button(-text=>'plus', -command=>sub {plus()})->pack();
my $lbl=$mw->Label(-textvariable=>\$total)->pack();
MainLoop;
sub plus()
{
$total++;
$queue->enqueue(1);
$lbl->configure(-text=>$total);
}
| |
| zentara 2006-06-17, 8:14 am |
| On 8 Jun 2006 07:42:03 -0700, limon7@gmail.com wrote:
>I just write some code using Tk & Thread below. When I click the "plus"
>Button, the woker thread increase $total, but the Label , in the Boss
>thread, doesn't change with $total as I wish.
>Then I have to click the "get_total" button to get the correct value of
>$total for the Label, while I think it should change automaticly.
>How to crrect it ?
Qos showed you the answer, but I will mention what your error in design
is. Shared variables are not automatically updated across threads, you
need to actively read them in the main thread ( or any thread). And
-textvariables will not work across threads to auto-update normally.
So when you increment $count in the thread, no other thread is notified
that the change occurred. They don't know until they try to read it. So
in main you can setup a fast timer to read it on a regular interval.
my $updater = $mw->repeat(10, sub{
$lbl->configure(-text=>$total);
#or untested this may work with -textvariable
$total = $total; #force a read
});
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
|
|
|
|
|