in reply to Re^4: Threads question
in thread Threads question
The intent of my original post was to point out that the problem exists, and it can get complex (as thread code complexity rises). So I still say "watch your memory especially when objects are used in the thread code".
As far as the tie problem goes, it really is a mute point, because when you monitor the thread's shared variables you need a loop or timer to read them. But it is a tripping point for new threads programmers.
I did whip out an old Tk program to test it, Tk has a Trace module, to setup such ties. If you notice in the program below, the needle will not move with the Trace, UNLESS the main program explicitly reads(or changes) the variable. Uncomment the repeat statement to make it work.
In Tk, there is a way with Tk::Trace to force a tie, but I havn't tried it across threads.#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; my $v:shared=0; my $thread = threads->new( \&launch_thread )->detach; package Tk; use Tk::Trace; package main; use Tk; use constant PI => 3.1415926; my $mw = MainWindow->new; $mw->fontCreate('medium', -family=>'courier', -weight=>'bold', -size=>int(-14*14/10)); my $c = $mw->Canvas( -width => 200, -height => 110, -bd => 2, -relief => 'sunken', -background => 'black', )->pack; $c->createLine(100,100,10,100, -tags => ['needle'], -arrow => 'last', -width => 5, -fill => 'hotpink', ); my $gauge = $c->createArc( 10,10, 190,190, -start => 0, -style => 'arc', -width => 5, -extent => 180, -outline => 'skyblue', -tags => ['gauge'], ); my $hub = $c->createArc( 90,95, 110,115, -start => 0, -extent => 180, -fill => 'lightgreen', -tags => ['hub'], ); $mw->traceVariable(\$v, 'w' => [\&update_meter]); #uncomment the following line to make the tie work #interestingly {$v = $v} will not do it #$mw->repeat(50,sub{ $v += 0 }); my $text = $c->createText( 100,50, -text => $v, -font => 'medium', -fill => 'yellow', -anchor => 's', -tags => ['text'] ); $c->raise('needle','text'); $c->raise('hub','needle'); MainLoop; sub update_meter { my($index, $value) = @_; if($value <= 0){$value = 0 } if($value >= 100){$value = 100} my $pos = $value / 100; my $x = 100.0 - 90.0 * (cos( $pos * PI )); my $y = 100.0 - 90.0 * (sin( $pos * PI )); $c->coords('needle', 100,100, $x, $y); $c->itemconfigure($text,-text => $value); return $value; } sub launch_thread{ while(1){ $v = 50 + int rand 50; print "$v\n"; select(undef,undef,undef,.5); } }
So my general point is that threads in Perl is NOT as clean as threads in c, and "sometimes" a thread-reuse scheme is needed. The tie problem is really not worth trying to work out, since a timer is so easy to setup
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Threads question
by BrowserUk (Patriarch) on Jun 30, 2007 at 13:28 UTC | |
by zentara (Cardinal) on Jun 30, 2007 at 16:30 UTC | |
by BrowserUk (Patriarch) on Jun 30, 2007 at 17:13 UTC |