[Chicago-talk] creating a thread on an object's method?
Jay Strauss
me at heyjay.com
Fri Dec 5 17:04:27 CST 2003
How would I create the thread on an object's method? Currently I have:
my $reader = Reader->new({api=>$api});
my $sub = $reader->can('go');
my $t = threads->new($sub, $reader);
but if I do:
my $t = threads->(\$reader->go)
it just executes $reader->go
thanks
Jay
below is the whole code
#!/opt/perl/perl-5.8.2/bin/perl -w
use strict;
package API;
use base qw/Class::Accessor/;
__PACKAGE__->mk_accessors(qw/data/);
sub update {
my $self = shift;
my $value = shift;
$self->data->{value} = $value;
}
package Reader;
use base qw(Class::Accessor);
__PACKAGE__->mk_accessors(qw/api/);
sub go {
my $self = shift;
my $i = 0;
while (1) {
$self->api->update($i++);
last if $self->api->data->{end};
}
}
package TWS;
use threads;
use threads::shared;
use base qw(Class::Accessor);
__PACKAGE__->mk_accessors();
sub connect {
my $self = shift;
my $api = shift;
my $reader = Reader->new({api=>$api});
my $sub = $reader->can('go');
my $t = threads->new($sub, $reader);
}
my %data : shared;
my $api = API->new({data=>\%data});
my $tws = TWS->new({api=>$api});
$tws->connect($api);
sleep 1;
for (1..5) {
print "in parent: ",$api->data->{value},"\n";
sleep 1;
}
$api->data->{end} = 1;
sleep 1;
More information about the Chicago-talk
mailing list