This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: pthread_cond_timedwait vs pthread_mutex_timedlock


On Mon, May 4, 2009 at 2:15 AM, Borislav Trifonov <trifonov@cs.ubc.ca> wrote:
> It's not clear to me which one I should use. ?I have a log-flushing thread
> that I need to wake every X seconds and flush the log (and also wake when
> signaled by a full log buffer or log destructor--by unlocking the mutex if I
> had used a mutex, or signaling the condition if I had used that).
>
> What is most important to me is to guarantee that no busy wait will occur.
>
> A possible complication is that I need my code to build on both Linux and
> Windows, so I'm using pthreads for Win32/64 in the latter case.

You will have to determine the intersection of supported functions
between pthreads and glibc's pthreads, and choose from that.

I would recommend pthread_cond_timedwait and pthread_cond_signal,
since it was designed for this type of work, and it's easy to loop
e.g. take the mutex, wait on the condition, do work, repeat, with
another thread signaling.

I know nothing about the pthreads for Win32/64 implemenation. POSIX
does not guarantee that an implementation will not use busy waits, you
will have to read the source code.

Any linux glibc port using NPTL (not Linuxthreads) is certainly free
of busy waits since the primitives use kernel futexes which are
busy-wait free.

Cheers,
Carlos.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]