Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: $threads->join() only returns one argument??? BUG?

by BrowserUk (Patriarch)
on Jun 11, 2003 at 22:33 UTC ( [id://265212]=note: print w/replies, xml ) Need Help??


in reply to $threads->join() only returns one argument??? BUG?

I'm not sure if it counts as a bug, but I encountered the same thing and couldn't find anything in the docs that suggested it should or shouldn't be able to return a list.

The simplest solution I came up with was join the list on the return and split it at the other end.

use threads; my $t = threads->create( sub{ sleep 10; return join $;, 'fred', 1, 1.123 } ); print split $;, $t->join;

Usual caveats about choice of seperator apply. $; seems as good a choice as any for most things.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Replies are listed 'Best First'.
Re: Re: $threads->join() only returns one argument??? BUG?
by djantzen (Priest) on Jun 11, 2003 at 23:19 UTC

    perlthrtut explicitly says that you can return a list, but the example code doesn't work as advertised:

    use threads; $thr = threads->new(\&sub1); @ReturnData = $thr->join; print "Thread returned @ReturnData"; sub sub1 { return "Fifty-six", "foo", 2; }

    prints "Thread returned 2". It looks like if the return statement returns a list you'll get the last element, and if you wrap it up inside an array (e.g., return @array = ("Fifty-six", "foo", 2)) it'll put it in scalar context no matter what the lvalue of the join(). Weird.

    Another option is to return an array reference and assign it to a scalar left of the join, or to pass a reference to a shared array into the threaded sub and have it populated.

    use threads; use threads::shared; share(@array); $thr = threads->new("sub1"); @ReturnData = $thr->join; print "Thread returned @ReturnData\n"; print "Thread populated @array\n"; sub sub1 { @array = ("Fifty-six", "foo", 2) }

    prints "Thread returned 3" and "Thread populated Fifty-six foo 2".


    "The dead do not recognize context" -- Kai, Lexx

      I missed that bit. Truth is I've never read perlthrtut beyond the first couple of paras because it seemed to be dealing with threads at a very general level which I'm already reasonably comfortable with. It also goes into way to much detail about all the different flavours of threads which perl doesn't use for my taste.

      There definitely seem to be a few bugs still in the 5.8 implementation. Much of it deriving from the inherently non-reentrant nature of the perl core from what I can work out. There's also a lack of good sample code right now.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


        I got stuck on this too when a while back and got help from the other Monks... perhaps this node will be helpful to you...Node: 231527
        This post shows a mult-threaded PING and uses shared variables between the threads.
        James

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found