in reply to multithreads newbie question

This meets the spec, though probably not your requirements:

#! 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(); async { sub3(); }->detach; sub4(); }; my $t2 = async{ sub2(); async{ sub5(); }->detach; sub6(); }; $_->join for $t1, $t2; my $t3 = async{ sub7() }; sub8(); $t3->join; 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 sub4 starts sub3 starts sub4:1 sub3:1 sub2 ends sub6 starts sub6:1 sub5 starts sub5:1 sub4:2 sub3:2 sub6:2 sub5:2 sub3:3 sub4:3 sub6:3 sub5:3 sub4 ends sub3 ends sub6 ends sub5 ends sub8 starts sub8:1 sub7 starts sub7:1 sub8:2 sub7:2 sub8:3 sub7:3 sub8 ends sub7 ends 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.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: multithreads newbie question
by roboticus (Chancellor) on Aug 10, 2010 at 13:01 UTC

    BrowserUk:

    Perhaps I should remove the phrase "something simple like this" from my post, as your example is complete *and* simpler. I still like mine for the flexibility, though, but yours is a better simple example of how to solve the problem.

    ...roboticus

      I think the main advantage (I see), of mine over yours is that there can be no race conditions, priority inversions, deadlocks, or any of the other nasties that thread naysayers like to run on about.

      People tend to scoff when I suggest the best way to avoid such things is to not program them.


      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.

        BrowserUk:

        Yes, I totally agree. You shouldn't put in junk you don't need for the job at hand.

        I tend to overengineer things, and have to reign myself in. Luckily, when I'm at $work, I spend a bit of time during design phase and explicitly chop out features that aren't supported by the requirements of the job.

        ...roboticus

Re^2: multithreads newbie question
by daverave (Scribe) on Aug 10, 2010 at 13:21 UTC
    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 :)

      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.