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

Hi monks ;D I've started at Perl some months ago, and I'm stuck in the following situation. I'm trying to set a result from inside a Thread and I'm not able to do this. The code below shows how I'm trying this:
use threads; use strict; my $returnn = ""; threads->new(sub { sleep(3); print "Run the thread! Variable should be set\n"; $returnn = "bla ble bli"; }); while (1 == 1) { if ($returnn ne "") { print "It works!\n"; } else { print "Doesn't work at all... :(\n"; } sleep(1); }

Replies are listed 'Best First'.
Re: How do I get a result from a thread?
by rafl (Friar) on Mar 20, 2006 at 15:15 UTC

    Either you need to mark the $returnn variable as shared between threads so changes are visible to other threads as well or, which is the better way in my eyes, return the result value in the code that runs inside the thread you spawn and use join() afterwards to get the result.

    use strict; use warnings; use threads; my $thread = threads->create(sub { sleep 3; return "bla ble bli"; }); print "Result: ", $thead->join(), "\n";

    Also take a look at the perlthrtut document.

    Cheers, Flo

Re: How do I get a result from a thread?
by zentara (Cardinal) on Mar 20, 2006 at 16:32 UTC
    Yeah, you are making the assumption that a thread sub code block returns automatically, you need to join it, to get the return value.
    #!/usr/bin/perl use warnings; use threads; use strict; $|=1; #turn off buffering my $returnn = ""; my $thread = threads->new(sub { print "Run the thread! Variable should be set\n"; sleep(3); $returnn = "bla ble bli"; }); my $result = $thread->join; print $result,"\n";
    Notice that the join statement, will block the main program until the thread finishes.

    There are other alternatives,i if you want to detect when the thread finishes, using threads::shared. Here I also detach the thread, so you don't need to join it.

    #!/usr/bin/perl use warnings; use threads; use threads::shared; use strict; $|=1; #turn off buffering my $returnn : shared; #declare as shared before setting value $returnn = ''; my $thread = threads->new(sub { print "Run the thread! Variable should be set\n"; sleep(3); $returnn = "bla ble bli"; })->detach(); while(1){ if($returnn eq ''){ print "No return yet\n"; sleep 1; }else{ print "return->$returnn\n"; last; } } print "Hit enter to exit\n"; <>;

    I'm not really a human, but I play one on earth. flash japh
      Thanks a lot all of you. Now it works. :DDDDDD =*****
Re: How do I get a result from a thread?
by McDarren (Abbot) on Mar 20, 2006 at 15:23 UTC
    <disclaimer>I've never worked with threads before, so what I have to say here may be a little off-track</disclaimer>

    However.... your question prompted me to have a look at perldoc threads, in which I found:

    It is very important to note that variables are not shared between threads, all variables are per default thread local. To use shared variables one must use threads::shared.
    So.... after adding the following two lines to your code:
    use threads::shared; share($returnn);
    ...I got output as follows:
    Doesn't work at all... :( Doesn't work at all... :( Doesn't work at all... :( Run the thread! Variable should be set It works! It works! It works! ...etc
    Which I kindof suspect is what you were after, yes?

    Cheers,
    Darren :)