Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hi all,

I have a problem with some perl code I need to write for sending DMX data to an Open-Lightning-Architecture-Daemon (that's for lightning of stages and so on).

I have written a class which connects to the "olad" and starts a background thread which sends regulary DMX-data to the daemon. For changing the DMX-data I have accessors defined.

My problem is, that the thread does no see changes inside the DMX-data. I started the thread with a reference to $self as an argument and inside the thread I use the argument as $super. I can dump all data from $super but if something outside the thread changes the data, this is not seen inside the thread. It seems that the thread has got a copy of %self instead of a reference.

I also tried to use shared(%self) - but I don't know how to access this data.

Here is my (test) code (TTTT.pm) withoud shared()-tests:

#/usr/bin/perl package TTTT; use threads; use threads::shared; use Data::Dumper; sub new { my $class=shift; my $self={ universe=>shift }; $self->{'_dmx_channels'}=chr(0)x512; $self->{'_thread'}=threads->create(\&worker_thread,\$self); return(bless(\%self, $class)); } sub set { my($self,$channel,$value)=@_; vec($self->{'_dmx_channels'},$channel,8)=$value; } sub worker_thread { my ($super)=@_; while(1) { $i++; print "Thread: $i:".$$super->{'universe'}."\n"; for($n=0;$n<30;$n++) { print int(vec($$super->{'_dmx_channels'},$n,8) +).","; } print "\n"; sleep(2); } } 1;

I tried to use the "set"-function and I tried to change the universe - the main program shows the changes, the thread does not.

What I am doing wrong?

Thanks, Holger


In reply to Access to $self in a thread? by Codeman_Rhodes

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (9)
    As of 2014-08-29 00:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (275 votes), past polls