On 6/23/07, Michael Scondo <[EMAIL PROTECTED]> wrote:

sub thread1{
                print "1\n";
                lock $x;
                print "locked x: 1\n";
                cond_wait $x;

Can't get past here until $x is signalled by another thread, and
unlocked by all other threads.

                print "thread1\n";
                lock $y;
                cond_signal $y;
                print "t1\n";
}

sub thread2{
                sleep 1;
                lock $y;
#               {
                        lock $x;
                        print "locked x: 2\n";
                        cond_signal $x;
#               }

If the curly braces of that naked block were present, the lock on $x
would be released here, at the end of that scope. But it's still
locked, because cond_signal didn't unlock it either. (Did you think it
would?)

                print "thread2\n";
                sleep 1;
                cond_wait $y;

Can't get past here until $y is signalled by another thread, and
unlocked by all other threads.

                print "t2\n";
}

As written, this is the end-of-scope for the lock on $x (as well as
the one on $y). But unless some third thread can rescue these first
two, they seem to be deadlocked, with the first waiting for $x to be
unlocked and the second waiting for $y to be signalled.

As you discovered, using the inner braces will release the lock on $x
soon enough to avoid the deadlock.

By the way, thanks greatly for the detailed, self-contained test case;
it showed exactly what you were doing.

Have fun with Perl!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to