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

Hi Gurus,

I have a program where I am creating 2 child threads from the main thread. I assume(d) that the main thread is supposed to continue doing its work, while the child threads do theirs.

But, in my program, I see that the main thread activities (some print statements, checks, etc.) are not being displayed ...and I see output only from the child threads.

Here's my question : Do the child threads block the main thread from executing until they are "join"ed?

A) If yes, is there a possible way to execute stuff that the main thread is supposed to do in parallel with child threads?

B) If no, then, why am I not seeing any output/activity from the main thread?

C) How can I make the main program do its work along with the child threads?

I have read from Perl Thread Tutorials (http://perldoc.perl.org/perlthrtut.html)that there are different models that can be used - boss/worker model, worker crew model, etc. I don't see any implementation example of these anywhere in perldoc.

Please let me know how I should proceed

  • Comment on Will Child Threads block the parent thread?

Replies are listed 'Best First'.
Re: Will Child Threads block the parent thread?
by dasgar (Priest) on Feb 27, 2011 at 05:50 UTC

    First of all, it's usually somewhere between very difficult and impossible for others to provide assistance about problems in your code when you don't share any code for others to look at. (See How do I post a question effectively?)

    As for your main question (Do the child threads block the main thread from executing until they are "join"ed?), the shortest answer is yes and no. Merely creating a child thread does not block the parent from executing code. However, once the parent code calls the join method on a child thread, it will sit and wait until the child thread finishes before continuing.

    Since you asked for an example, here's some example code.

    use strict; use warnings; use threads; my $thread = threads->create(\&child); for (1 .. 10) { sleep 1; print "parent\n"; } $thread->join(); sub child { for (1 .. 5) { sleep 1; print "child\n"; } }

    The above code produced the following output:

    child parent child parent child parent child parent child parent parent parent parent parent parent

    As you can see, the parent and child were both running at the same time. Had I put the join statement in front of the for loop in the main code section, the parent would have waited for the child thread to complete before entering the main for loop.

    Hopefully this helps to clarify things for you. If not, come back and post your code.

      Had I put the join statement in front of the for loop in the main code section, the parent would have waited for the child thread to complete before entering the main for loop

      Yikes!!!! I had done just that!!!! I had my "join" statement as soon as I had created the child thread - obviously, Perl waited for the child to finish and only then, it allowed the main program to do its work. I have put the join statement at the end of the code now ... Let's see if that works!

      As far as code goes, my code is too huge to put in on this page.. I mean.. many subroutines being used and to make complete sense, I had to put in all of that code here... Just needed to understand the concept, and I realized what I had done wrong...

      Btw, are there any other web pages / tutorials / books / chapters that make us understand Threads in Perl in a better way? PerlDoc has very few examples and it's difficult to understand some concepts ... I'm really not very happy with the perldoc help pages.

        As far as code goes, my code is too huge to put in on this page.

        I would strongly suggest that you write a few small programs to explore how threading works before starting to use them in large programs. To do otherwise is to almost guarantee that you will create yourself a lot of work and pain trying to get the basics right.


        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.

        So you had a fundamental problem (join in the wrong place) that you could have demonstrated with 10 - 20 lines of code, and probably have found the problem yourself in the process of generating the sample code. Why would you want to post a ton of cruft just to show that issue (see I know what I mean. Why don't you?)?

        True laziness is hard work
        Btw, are there any other web pages / tutorials / books / chapters that make us understand Threads in Perl in a better way? PerlDoc has very few examples and it's difficult to understand some concepts ... I'm really not very happy with the perldoc help pages.

        I'm personally unaware of other available documentation. That doesn't necessarily mean that they don't exist.

        At times I get confused about the documentation for a new function/module that I'm trying to use. What I usually do is very similar to BrowserUk's suggestion. Just write some small test code to test out the function/module to see if I understand what it is doing and how I'm supposed to use it. Sometimes I personally find that kind of experimentation to be far more useful to me in the learning process than the documentation.

        In fact, even if I think I completely understand the documentation, I'll still do some small experimental code just to verify if I got it right or not. As BrowserUk suggested, it's much easier to debug that small experimental code while trying to learn the new stuff. And once you have figured things out with the small experimentation code, then you're ready to implement it in the real code that you're trying to develop.

Re: Will Child Threads block the parent thread?
by cdarke (Prior) on Feb 27, 2011 at 05:55 UTC
    Do the child threads block the main thread from executing until they are "join"ed?

    No.

    B) If no, then, why am I not seeing any output/activity from the main thread?

    Without seeing your code it is difficult to say. A common issue is buffering. That is, you cannot see anything happening because output is being written to an in-memory buffer which does not get flushed until it is closed, unless the program explicitly flushes it. If you make STDOUT unbuffered using local $|=1; then that might help.
Re: Will Child Threads block the parent thread?
by GrandFather (Saint) on Feb 27, 2011 at 05:56 UTC

    Care to show us a small sample script illustrating the problem? The answer is 'no', the children do not block the parent if things are done right, but that may not be the case with your code.

    True laziness is hard work