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

Access to $self in a thread?

by Codeman_Rhodes (Initiate)
on Sep 12, 2013 at 13:24 UTC ( #1053722=perlquestion: print w/ replies, xml ) Need Help??
Codeman_Rhodes has asked for the wisdom of the Perl Monks concerning the following question:

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

Comment on Access to $self in a thread?
Download Code
Re: Access to $self in a thread?
by RichardK (Priest) on Sep 12, 2013 at 13:48 UTC

    You don't seem to be sharing anything.

    try reading the section on objects in the docs for threads::shared, I've never done this so I can't suggests anything else.

Re: Access to $self in a thread?
by BrowserUk (Pope) on Sep 12, 2013 at 15:51 UTC

    Simply stated, you cannot do what you are trying to do. That is, you cannot share objects between threads.

    I'm not going to explain that, but just ask you to take my word for it.

    I'm also going to suggest that given your current level of understanding of Perl, you should not even be considering using threads.

    An example of what I mean is your new function.

    • It creates a scalar called $self to which it assigns a reference to an anonymous hash.
    • It then passes a reference to that reference into a thread.

      That thread will get a copy of the reference to a scalar, which will be a copy of the scalar $self which will point to a copy of the anonymous hash containing a copy of the key 'universe' and a copy of whatever value was passed in to initialise that value.

      Nothing you do to any of the copies, will ever be visible to the other.

    • The function ends by blessing and returning a reference to a completely different, undeclared, and uninitialised hash called %self.

      %self has no relationship to the scalar called $self.

    Given your demonstrated level of understanding of Perl; your attempts to use threading will just fail.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Access to $self in a thread?
by Anonymous Monk on Sep 12, 2013 at 20:14 UTC
    Having a worker-thread while(1) { ... sleep() } is also not what you want to do . . .
      Hi all, thanks for your replies. I see - I have to go another way to solve my problem. Regards, Holger

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1053722]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-12-28 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (179 votes), past polls