git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] threaded pack-objects: Use condition variables for thread communication.

From
Dmitry Potapov <dpotapov@gmail.com>
Date
Dec 17, 2007, 04:26 UTC
Message-ID
<20071217042648.GI14173@dpotapov.dyndns.org>
In-Reply-To
<20071216190016.GC4999@xp.machine.xx>
On Sun, Dec 16, 2007 at 08:00:16PM +0100, Peter Baumann wrote:
Show 23 quoted lines
> On Sun, Dec 16, 2007 at 07:41:37PM +0100, Johannes Sixt wrote:
> > On Sunday 16 December 2007 13:05, Peter Baumann wrote:
> > > On Sun, Dec 16, 2007 at 12:18:53AM +0100, Johannes Sixt wrote:
> > > > +
> > > > +		progress_lock();
> > > > +		me->working = 0;
> > > > +		progress_unlock();
> > > > +		pthread_cond_signal(&progress_cond);
> > >
> > > Shouldn't the pthread_cond_signal be inside the lock?
> > > e.g. swap progress_unlock() with pthread_cond_signal(&progress_cond)
> > 
> > No, that's not necessary. Both ways are correct, but if it's outside the lock 
> > there is less contention on the mutex (because the waiting thread must 
> > acquire the mutex lock before it can return from pthread_cond_wait).
> > 
> 
> At least I was told otherwise and [1] backs my knowledge up. Are you
> really sure?
> 
> -Peter
> 
> http://docs.sun.com/app/docs/doc/806-5257/6je9h032r?a=view#sync-53686

The POSIX standard clearly says that usage of pthread_cond_signal ouside of the mutex protection is allowed:

=== The pthread_cond_signal() or pthread_cond_broadcast() functions may be called by a thread whether or not it currently owns the mutex that threads calling pthread_cond_wait() or pthread_cond_timedwait() have associated with the condition variable during their waits; however, if predictable scheduling behaviour is required, then that mutex is locked by the thread calling pthread_cond_signal() or pthread_cond_broadcast(). === http://www.opengroup.org/onlinepubs/007908775/xsh/pthread_cond_signal.html

And the argumentation provided by Sun's manual:
> Otherwise, the condition variable could be signaled between the test
> of the associated condition and blocking in pthread_cond_wait(), which
> can cause an infinite wait.

sounds strange to me. Indeed the condition variable could be signaled between the test and blocking, but the condition itself is false, so the thread should be blocked anyway. The only effect of signalling outside of the mutex protection is that the thread blocked on the condition variable can be waken up while the associated condition is still false, but it is not a problem.

As to what is more optimal, it is less clear. Because on one side, signaling under the lock increases contention, but, on the other hand, it avoids spurious wakeup. So, I suppose it could be implementation specific.

Dmitry
Previous: Johannes SixtNext: David Brown
Message 10 of 11 in “threaded pack-objects: Use condition variables for thread communication.”
  1. threaded pack-objects: Use condition variables for thread communication.Johannes Sixt, Dec 15, 2007
  2. Peter BaumannDec 16, 2007
  3. Johannes SixtDec 16, 2007
  4. Peter BaumannDec 16, 2007
  5. threaded pack-objects: Use condition variables for thread communication.Johannes Sixt, Dec 16, 2007
  6. Junio C HamanoDec 16, 2007
  7. Nicolas PitreDec 17, 2007
  8. Johannes SixtDec 17, 2007
  9. Plug a resource leak in threaded pack-objects code.Johannes Sixt, Dec 17, 2007
  10. Dmitry PotapovDec 17, 2007
  11. David BrownDec 16, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.