/* * This program is used to verify that when the owner of a non-robust lock exit * prematurely without unlocking the mutex, the next owner will be block * indefintely. */ #include #include #include #include pthread_mutex_t lock; void *original_owner_thread(void *ptr) { printf("[original owner] Setting lock...\n"); pthread_mutex_lock(&lock); printf("[original owner] Locked. 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); /* initialize the attribute object */ /* the following line can be commented out because PTHREAD_MUTEX_STALLED is * the default value for a mutex attribute object */ pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_STALLED); pthread_mutex_init(&lock, &attr); /* initialize the lock */ pthread_create(&lock_getter, NULL, original_owner_thread, NULL); sleep(2); /* original_owner_thread should have exited now */ printf("Attempting to acquire unlock robust mutex.\n"); int ret_code = pthread_mutex_lock(&lock); if(EOWNERDEAD == ret_code) { printf("EOWNERDEAD returned. Make the mutex consistent now\n"); pthread_mutex_consistent(&lock); } pthread_mutex_unlock(&lock); return 0; }