CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Accessing class data from a second thread

 



clockwrkz
New User

Jun 16, 2011, 10:20 PM

Post #1 of 1 (449 views)
Accessing class data from a second thread Can't Post

Hi,

I'm trying to access the data held in a class in the main thread of the application from a second thread. The second thread allows a single client to connect and query the information in the class.

I understand that the variables aren't locked yet but I'm not at the point to worry about that yet.

I can call functions from the class but the data is empty since the second thread simply has a copy of the class in shared memory but no access to the current data in the class.

Here is a cut down code I'm looking for help with:


Code
################################################################# 
# This package is an actual found tag

package Tag;
sub new {
my $class = shift;
my $self = {
_TagID => shift,
_ProductName => shift,
_ProductDesc => shift,
_ProductPrice => shift,
};
bless $self, $class;
if (0) {
print "_TagID:".$self->{_TagID} . "\n";
print "_ProductName:".$self->{_ProductName} . "\n";
print "_ProductDesc:".$self->{_ProductDesc} . "\n";
print "_ProductPrice:".$self->{_ProductPrice} . "\n";
}
return $self;
}

#################################################################
# This package will contain a list of tags. It will handle
# managing and expiring tags

package TagList;
sub new {
my $class = shift;
my @TagList = ();
my $self = {
'TagList' => \@TagList,
};
bless $self, $class;
return $self;
}

sub NumberOfTags {
my ($self, $value) = @_;
return @{$self->{'TagList'}};
}


#################################################################

package main;
my $CurrentTagList = TagList->new();
bless($CurrentTagList, 'TagList');
my $thr = threads->create(\&http_listen);

while() {
# read and process tags to fill $CurrentTagList
# gets data from a client at a 200 ms interval
}


#################################################################
# functions for HTTP thread
# this will eventually allow a single client to connect and get the tag information at it's own interval

sub http_listen {
my $http_lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080, ReuseAddr => 1) || die "Error creating socket: $!";
my $http_sel = new IO::Select( $http_lsn );
my $line = "";
while (my @ready = $http_sel->can_read) {
foreach my $fh (@ready) {
if ($fh == $http_lsn) {
my $new = $http_lsn->accept;
$http_sel->add($new);
my $host = $new->peerhost;
print "[Accepting HTTP connection from $host]\n";
} else {
$line = <$fh>;
print $line;
print 'HTTP - There are ' . $CurrentTagList->NumberOfTags() . ' tags' . "\n\n";
sleep(1);
}
}
}
}

#################################################################


In the above example, in http_listen $CurrentTagList->NumberOfTags() is always = 0 as you would expect. But, my question is, how do I allow the http_listen function to access the current data held in $CurrentTagList in the main thread?

Thanks for any and all help

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives