Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^2: Is returning a list broken when using threads?

by beable (Friar)
on Jul 25, 2004 at 02:55 UTC ( #377220=note: print w/replies, xml ) Need Help??

in reply to Re: Is returning a list broken when using threads?
in thread Is returning a list broken when using threads?

Riiiiight, so this example from perlthrtut is rather misleading then:
Waiting For A Thread To Exit Since threads are also subroutines, they can return values. To wait for a thread to exit and extract any values it might return, you can use the join() method: use threads; $thr = threads->new(\&sub1); @ReturnData = $thr->join; print "Thread returned @ReturnData"; sub sub1 { return "Fifty-six", "foo", 2; } In the example above, the join() method returns as soon as the thread ends. In addition to waiting for a thread to finish and gathering up any values that the thread might have returned, join() also performs any OS cleanup necessary for the thread. That cleanup might be impor- tant, especially for long-running programs that spawn lots of threads. If you don't want the return values and don't want to wait for the thread to finish, you should call the detach() method instead, as described next.

Replies are listed 'Best First'.
Re^3: Is returning a list broken when using threads?
by BrowserUk (Patriarch) on Jul 25, 2004 at 03:43 UTC

    I think the problem with perlthrtut and some other bits of the documentation (what there is of it), was mostly written for 5005threads and hasn't really caught up with ithreads yet.

    That said. I'm not sure how the snippet you quote would have worked then either.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re^3: Is returning a list broken when using threads?
by tilly (Archbishop) on Jul 25, 2004 at 04:59 UTC
    Patches are welcome. (Appreciated even.)
      Just a wild idea: could the thread routine not provide the return result for all possible contexts (scalar, array, boolean, ...) and have the join routine pick the one it needs as per its own context?


      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

        That could be tricky, unless there were some guarantee that calling a routine in multiple contexts is idempotent.

        I thought that at first, but then I realized, as I mentioned above, that there's still one problem: what should wantarry return if called from within the designated subroutine?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2023-03-21 03:51 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.