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

I've just started learning perl threads (ithreads), and looks like this example from perlthrtut isn't working:
use threads; $thr = threads->new(\&sub1); @ReturnData = $thr->join; print "Thread returned @ReturnData"; sub sub1 { return("Fifty-six", "foo", 2); }
It return "2" instead of ("Fifty-six", "foo", 2) - looks like thread is executed in scalar context.

So, is this a bug in join(), or just broken example?

Replies are listed 'Best First'.
Re: threads->join
by liz (Monsignor) on Oct 05, 2003 at 21:52 UTC
    I think it is a documentation problem in perlthrtut. The documentation of threads->join states specifically:
    $thread->join
    This will wait for the corresponding thread to join. When the thread finishes, join() will return the return values of the entry point function. If the thread has been detached, an error will be thrown.

    The context (scalar or list) of the thread creation is also the context for join(). This means that if you intend to return an array from a thread, you must use "my ($thread) = threads-"new(...)>, and that if you intend to return a scalar, you must use "my $thread = ...".

    Applied to your example, this would mean:

    ($thr) = threads->new(\&sub1);

    Liz

    Update:
    Apparently the above documentation is in 5.8.1, and not in 5.8.0. I guess you could say it's one more documentation bug that was fixed by 5.8.1. Sorry for the possible confusion.

      Wow. Thanks. But... I've checked threads module documentation before post here! Here is it:

      $thread = threads->create(function, LIST)
      This will create a new thread with the entry point function and give it LIST as parameters. It will return the corresponding threads object. The new() method is an alias for create().
      $thread->join
      This will wait for the corresponding thread to join. When the thread finishes, join() will return the return values of the entry point function. If the thread has been detached, an error will be thrown. If the program exits without all other threads having been either joined or detached, then a warning will be issued. (A program exits either because one of its threads explicitly calls exit(), or in the case of the main thread, reaches the end of the main program file.)

      I'm using perl 5.8.0 and this is text from `perldoc threads`. So, where you find your version of this documentation?