Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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 ( 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

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (11)
    As of 2017-11-21 15:49 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (304 votes). Check out past polls.