mastermagrath
Technical User
Hi folks,
Thanks for all your help and pointers, however i'm stuck again!! Can someone please please look at the small script i've written and point out why it wont work?! Basically i create a thread which prints hashes at $period seconds interval. $period is a shared variable. I expect the thread to go off and do its stuff. Then i create a slider widget that allows the user to change the value of $period.
The slider by itself works perfectly, the thread by itself works perfectly. However when i put them together, once the thread is created the hash printing loop seems to tie up the script and the widget is never created. This confuses me as i thought the whole point in threads was to allow parallel processing?
#C:\perl\bin\perl.exe -w
use Tk;
use strict;
use FileHandle;
use warnings;
use threads;
use threads::shared;
my $period : shared = 1;
my $thr = threads->new(\&hashes)->join;
$thr->detach;
sub hashes {
while () {
print "#";
flush STDOUT;
sleep $period;
}
}
my $scale_value = '1';
my $message = "Your period is $scale_value";
my $mw = MainWindow->new;
$mw->title('Scale');
my $label = $mw->Label(-textvariable => \$message);
my $age = $mw->Scale(-label => 'Age',
-variable => \$scale_value,
-orient => 'horizontal',
-from => 1,
-to => 9,
-resolution => 1,
-tickinterval => 20,
-command => \&display);
my $exit = $mw->Button(-text => 'Exit',
-command => [$mw => 'destroy']);
$label->pack;
$age->pack;
$exit->pack;
MainLoop;
sub display {
$message = "Your period is $scale_value";
$period = $scale_value;
}
Thanks for all your help and pointers, however i'm stuck again!! Can someone please please look at the small script i've written and point out why it wont work?! Basically i create a thread which prints hashes at $period seconds interval. $period is a shared variable. I expect the thread to go off and do its stuff. Then i create a slider widget that allows the user to change the value of $period.
The slider by itself works perfectly, the thread by itself works perfectly. However when i put them together, once the thread is created the hash printing loop seems to tie up the script and the widget is never created. This confuses me as i thought the whole point in threads was to allow parallel processing?
#C:\perl\bin\perl.exe -w
use Tk;
use strict;
use FileHandle;
use warnings;
use threads;
use threads::shared;
my $period : shared = 1;
my $thr = threads->new(\&hashes)->join;
$thr->detach;
sub hashes {
while () {
print "#";
flush STDOUT;
sleep $period;
}
}
my $scale_value = '1';
my $message = "Your period is $scale_value";
my $mw = MainWindow->new;
$mw->title('Scale');
my $label = $mw->Label(-textvariable => \$message);
my $age = $mw->Scale(-label => 'Age',
-variable => \$scale_value,
-orient => 'horizontal',
-from => 1,
-to => 9,
-resolution => 1,
-tickinterval => 20,
-command => \&display);
my $exit = $mw->Button(-text => 'Exit',
-command => [$mw => 'destroy']);
$label->pack;
$age->pack;
$exit->pack;
MainLoop;
sub display {
$message = "Your period is $scale_value";
$period = $scale_value;
}