Roger Oberholtzer wrote:
> On Mon, Jun 4, 2018 at 8:45 AM, Per Jessen <per(a)computer.org
> <mailto:email@example.com>> wrote:
> I'm reviving some old code, dates back to 2010. In one place I see I
> have replaced use of pthread_mutexes with sem_wait() and sem_post()
> (posix semaphores). I just can't see why I did it. :-(
> The semantics are slightly different, but can anyone suggest why one
> should use one over the other? (or vice versa).
> They would act differently. The pthread_mutexes typically wait for
> access to a variable. They do not wait for the variable itself to
> change. The sem_wait(), well, waits for the semaphore to change. Is the
> semaphore value itself the one you are interested in? Or does it's
> change mean that some variable you are interested in has changed? IMHO,
> pthread_mutexes are safer because when you access the protected
> variable, it should not be in the process of changing.
Thanks for your input, Roger.
Here is what I wrote -
//pthread_mutex_lock( &mux );
while( EINTR==sem_wait( &mux ) );
do some queue manip.
//pthread_mutex_unlock( &mux );
sem_post( &mux );
mux is either a pthread_mutex_t or a sem_t.
The queue manipulation is key, the mux is uninteresting. The reason I am
looking at this is that a thread got hung up (over the weekend) - in the core
dump, a signal handler was called and tried to write to the queue whilst someone
was reading from the queue. It's possible that is why I had to move to using
semaphores - I wonder if pthread_mutex_lock is async-safe.
To unsubscribe, e-mail: opensuse-programming+unsubscribe(a)opensuse.org
To contact the owner, e-mail: opensuse-programming+owner(a)opensuse.org