in reply to Will Child Threads block the parent thread?

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.

Replies are listed 'Best First'.
Re^2: Will Child Threads block the parent thread?
by vishi (Beadle) on Feb 27, 2011 at 08:46 UTC

    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.