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:

################################################################# # 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 ta +g 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


In reply to Accessing class data from a second thread by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.