develooper Front page | perl.perl5.porters | Postings from August 2012

Re: [perl #114378] cond_signal does not wake up a thread

Thread Previous
Dave Mitchell
August 2, 2012 08:35
Re: [perl #114378] cond_signal does not wake up a thread
Message ID:
On Thu, Aug 02, 2012 at 02:36:34AM -0700, Matti Linnanvuori wrote:
> cond_signal does not wake another thread up even though I think it should.
> The same problem appears in perl 5.10.0 in SLES 11 SP1.
> The main thread joins the other thread forever:

Its difficult to say for certain, since you don't provide complete code,
but it looks like the fault is here:

> lock %location;
> syslog LOG_NOTICE, "Signaling the post thread to terminate";
> cond_signal %location;
> if ( defined $thread ) {
>     syslog LOG_NOTICE, "Joining the post thread";
>     $thread->join();
> }

You don't unlock %location before trying to join the thread. Note that
cond_signal() itself does not unlock the variable (and so cond_wait()
hangs trying to relock the var).

So you probably need to wrap the lock and cond_signal lines above in a
{... } block, so that the lock is released before attempting to join

As a more general comment, I'd recommend try to avoid using cond_wait and
cond_signal wherever possible. They are a very low-level interface, and
are notoriously hard to get right. Use something higher level: often
a simple perl-level lock is sufficient (it uses condition variables under
the hood to ensure that a process waiting on a lock gets woken up),
or possibly something like Thread::Queue.

This is a great day for France!
    -- Nixon at Charles De Gaulle's funeral

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About