[NPTL docs] pthread robustness and pthread_mutex_consistent

Yubin Ruan ablacktshirt@gmail.com
Mon Aug 7 04:06:00 GMT 2017


Hi,

where can I find some docs of the NPTL lib on Linux? I am writing some
application with pthread, but not so sure what exactly
"pthread_mutex_consistent/pthread_mutex_consistent_np" does. I know
POSIX has their standard on this, but I am not so sure how will
Linux's implementation differ from that.

Currently I have a system, in which multiple threads are running and
try to lock/unlock to synchronize when they try to read/write a memory
area. However some threads might die inadvertently with the lock hold,
making the whole system deadlocked. So it would be great if I can make
sure that if a thread die, all the locks it held will be released.

Here is a naive example to show what I mean:

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>

    pthread_mutex_t lock;

    void dropped_thread(void)
    {
        printf("Setting lock...\n");
        pthread_mutex_lock(&lock);
        printf("Lock set, now exiting without unlocking...\n");
        pthread_exit(NULL);
    }

    int main(int argc, char *argv[])
    {
        pthread_t lock_getter;
        pthread_mutexattr_t attr;
        pthread_mutexattr_init(&attr);
        pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);

        pthread_create(&lock_getter, NULL, (void *) dropped_thread, NULL);
        sleep(2);

        printf("Inside main\n");
        printf("Attempting to acquire unlocked mutex?\n");

        pthread_mutex_consistent(&lock);
        pthread_mutex_unlock(&lock);
        pthread_mutex_lock(&lock);

        printf("Successfully acquired lock!\n");

        return 0;

    }

My question is:
  * how can I check whether a mutex is consistent (i.e., a thread die
with the mutex hold). Can I do this with only `pthread_mutex_lock()'
(i.e., when a thread die, another thread trying to lock it with
pthread_mutex_lock will automatically succeed, probably with some
error code returned)
  * can I do the check in any thread? Some people says that it is only
possible to do this in the main thread.

And, the maybe the last ultimate question: what does "np" means in
`pthread_mutex_consistent_np'? Native Posix as in NPTL? What is the
difference between a "np" and a "non-np" implementation?

Many thanks!
Yubin



More information about the Libc-help mailing list