Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Retreiving variables from threads

by JFarr (Sexton)
on Oct 28, 2005 at 21:48 UTC ( [id://503783]=perlquestion: print w/replies, xml ) Need Help??

JFarr has asked for the wisdom of the Perl Monks concerning the following question:

All mighty monks, Young prophet beeseaches you wisdom on all PERL :). I need to write a perl script that is multi-threaded. Not a problem. But withing these threads/subs I need to pull out a $var and a @array. Everything I have tried seems to fail, including createing a 'my var' at the top of the file. I've even went so far as to try and return a value from the threaded subroutine. Any help is greatly appreciated. example: $thr1 = new Thread \&mysub; ... sub mysub { ... $ddb = someting;# I need this returned to the main # routine so I can pass it to another #sub. ... } Similar situation for the array. Thanks in advance.

Replies are listed 'Best First'.
Re: Retreiving variables from threads
by kwaping (Priest) on Oct 28, 2005 at 22:01 UTC
Re: Retreiving variables from threads
by liz (Monsignor) on Oct 28, 2005 at 22:58 UTC

      Please don't bring this up again without updating the post to bring it up to date.

      Much of that nodes contents are no longer true. It also way over emphasises the downsides of iThreads.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Retreiving variables from threads
by bageler (Hermit) on Oct 28, 2005 at 23:25 UTC
    This code snippet will allow $var to be shared amongst threads:
    use threads::shared; my $var :shared;
Re: Retreiving variables from threads
by zentara (Archbishop) on Oct 29, 2005 at 11:27 UTC
    Usually it is a good idea to set the shared value too. This works for me.
    use threads; use threads::shared; my %shash; my @choices; my $x; share $shash{'go'}; share @choices; share $x; $shash{'go'} = 0; @choices = ( ); $x = 1;
    Also if you declare them as shared, after you initialize them, you will lose the initialization.

    Another tip is the main thread will not automatically read the shared variable (like thru a reference). You may need to actively read it in the main thread.


    I'm not really a human, but I play one on earth. flash japh
Re: Retreiving variables from threads
by JFarr (Sexton) on Oct 31, 2005 at 22:07 UTC
    Thanks for all the help so far, lets toss in another catch, this is running on a windows machine....are there any gatch ya's? If I do not call use "Thread;" I cannot do the $t1 = new Thread \&sub1; And if I include both "use Thread;" and "use threads;" I receive a lot of "...XXX is redfined..." It sucks being a noobie to Perl lol. My humble gratitude my masters.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://503783]
Approved by kwaping
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-18 06:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found