in reply to Re: multithreads newbie question
in thread multithreads newbie question

Thank you so very much. I think this does meet the requirements (why not)?

Just one thing I would like to make sure: when I use the main part of your code ($t1, $t2 and $t3) as a body of a function, and the function returns, do I know for sure that all threads are done? I read the documentation for detach and it scared me a little bit :)

Replies are listed 'Best First'.
Re^3: multithreads newbie question
by BrowserUk (Patriarch) on Aug 10, 2010 at 13:46 UTC
    I read the documentation for detach and it scared me a little bit :)

    So avoid detach and use a couple more joins:

    #! perl -slw use 5.010; use strict; use threads; sub sub1 { say"sub1 starts"; say("sub1:$_"),sleep 1 for 1..3; say"sub1 + ends" } sub sub2 { say"sub2 starts"; say("sub2:$_"),sleep 1 for 1..3; say"sub2 + ends" } sub sub3 { say"sub3 starts"; say("sub3:$_"),sleep 1 for 1..3; say"sub3 + ends" } sub sub4 { say"sub4 starts"; say("sub4:$_"),sleep 1 for 1..3; say"sub4 + ends" } sub sub5 { say"sub5 starts"; say("sub5:$_"),sleep 1 for 1..3; say"sub5 + ends" } sub sub6 { say"sub6 starts"; say("sub6:$_"),sleep 1 for 1..3; say"sub6 + ends" } sub sub7 { say"sub7 starts"; say("sub7:$_"),sleep 1 for 1..3; say"sub7 + ends" } sub sub8 { say"sub8 starts"; say("sub8:$_"),sleep 1 for 1..3; say"sub8 + ends" } my $t1 = async{ sub1(); my $t = async { sub3(); }; sub4(); $t->join; }; my $t2 = async{ sub2(); my $t = async{ sub5(); }; sub6(); $t->join }; $_->join for $t1, $t2; my $t3 = async{ sub7() }; sub8(); $t3->join; print "main ends";

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Actually I think there is one problem with this. sub7 and sub8 do not start as soon as possible. They wait for all the previous subs to finish (including sub3-sub6), instead of only for sub1 and sub2.

        Sorry, you're right. How did we miss that. How's this?

        #! perl -slw use 5.010; use strict; use threads; =commant sub_1 can run immediately sub_2 can run immediately sub_3 can run only after sub_1 finishes sub_4 can run only after sub_1 finishes sub_5 can run only after sub_2 finishes sub_6 can run only after sub_2 finishes sub_7 can run only after both sub_1 and sub_2 finish sub_8 can run only after both sub_1 and sub_2 finish =cut sub sub1 { say"sub1 starts"; say("sub1:$_"),sleep 1 for 1..3; say"sub1 + ends" } sub sub2 { say"sub2 starts"; say("sub2:$_"),sleep 1 for 1..3; say"sub2 + ends" } sub sub3 { say"sub3 starts"; say("sub3:$_"),sleep 1 for 1..3; say"sub3 + ends" } sub sub4 { say"sub4 starts"; say("sub4:$_"),sleep 1 for 1..3; say"sub4 + ends" } sub sub5 { say"sub5 starts"; say("sub5:$_"),sleep 1 for 1..3; say"sub5 + ends" } sub sub6 { say"sub6 starts"; say("sub6:$_"),sleep 1 for 1..3; say"sub6 + ends" } sub sub7 { say"sub7 starts"; say("sub7:$_"),sleep 1 for 1..3; say"sub7 + ends" } sub sub8 { say"sub8 starts"; say("sub8:$_"),sleep 1 for 1..3; say"sub8 + ends" } my $t1 = async{ sub1(); return async { my $t = async { sub3(); }; sub4(); $t->join; }; }; my $t2 = async{ sub2(); return async { my $t = async{ sub5(); }; sub6(); $t->join }; }; my @kids; push @kids, $_->join for $t1, $t2; push @kids, async{ sub7() }; sub8(); $_->join for @kids; print "main ends"; __END__ c:\test>854022.pl sub1 starts sub1:1 sub2 starts sub2:1 sub1:2 sub2:2 sub1:3 sub2:3 sub1 ends sub2 ends sub4 starts sub3 starts sub3:1 sub4:1 sub6 starts sub5 starts sub6:1 sub5:1 sub8 starts sub8:1 sub7 starts sub7:1 sub3:2 sub4:2 sub5:2 sub6:2 sub8:2 sub7:2 sub3:3 sub4:3 sub5:3 sub6:3 sub8:3 sub7:3 sub3 ends sub4 ends sub5 ends sub6 ends sub8 ends sub7 ends main ends