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 | |
by BrowserUk (Patriarch) on Feb 27, 2011 at 09:04 UTC | |
by GrandFather (Saint) on Feb 27, 2011 at 09:39 UTC | |
by dasgar (Priest) on Feb 27, 2011 at 13:38 UTC |