Threads and OO Question
From: Edward Wildgoose (Ed+nospam_at_ewildgoose.demon.co.uk@)
Date: 10/14/03
- Next message: Josef Möllers: "Re: Process each input record as an array of elements"
- Previous message: Glenn Jackman: "Re: Please help re Perl script modification"
- Next in thread: Bill: "Re: Threads and OO Question"
- Reply: Bill: "Re: Threads and OO Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 14 Oct 2003 14:41:34 GMT
I have a peculiar problem with threads on Perl 5.8 (activestate on Win32)
My sample code is below, however, the problem is basically that if I run
this under the debugger then if "works" as expected. Works in this case
defined as some output goes to stdout.
However, if I simply run the program from the command line, then I only get
output from the thread running "MyPackage->start"... There is no output
from the main thread. Any ideas why (does it happen to anyone else? Linux?)
Is this to do with one of the threads grabbing stdout and the other not
being able to access it?
Secondly is this the "right" way to do thread programming? The "problem" I
am trying to solve is to have the main start thread showing a GUI interface,
where the MyPackage class is then a worker class which goes off and does
something. Then the GUI will periodically access the properties of
"MyPackage" to determine state and progress (and update the GUI). FWIW,
there is a deliberate attempt to keep the two processes seperate hence not
relying on the any abilities the GUI toolkit might provide to do background
processing.
Thanks
Ed W
>>>>>>>>>>>>>>>>>>>>>>>>>>>>
use threads;
use threads::shared;
use strict;
package MyPackage;
my $counter : shared;
$counter = 0;
sub new {
my $class = shift;
my $this = {COUNTER => 0};
threads::shared::share($this->{COUNTER});
$counter = 0;
return bless($this, $class);
}
sub start {
my $this = shift;
while (1) {
$counter += 1;
$this->{COUNTER} += 1;
sleep 1;
print STDOUT "In Counter: $counter \r\n";
}
}
sub getCounter {
my $this = shift;
return $this->{COUNTER};
# return $counter;
}
package main;
sub MainLoop {
my $a;
my $a = MyPackage->new();
my $thr = threads->new(sub{$a->start();});
sleep 1;
while (1) {
my $count = $a->getCounter();
print STDOUT $count;
sleep 1;
}
my @ReturnData = $thr->join;
print "Thread returned @ReturnData";
}
MainLoop();
- Next message: Josef Möllers: "Re: Process each input record as an array of elements"
- Previous message: Glenn Jackman: "Re: Please help re Perl script modification"
- Next in thread: Bill: "Re: Threads and OO Question"
- Reply: Bill: "Re: Threads and OO Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|