Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Access to $self in a thread?

by Codeman_Rhodes (Novice)
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 ( 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

Replies are listed 'Best First'.
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 RichardK (Parson) 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 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1053722]
Front-paged by Corion
and the rats come out to play...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2017-03-25 12:38 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (311 votes). Check out past polls.