#include #include int main() { pthread_rwlock_t lk; pthread_rwlockattr_t lkattr; pthread_rwlockattr_t * attrs[2]; int i = 0, j = 0; attrs[0] = NULL; attrs[1] = &lkattr; if (0 != pthread_rwlockattr_init(&lkattr)) { printf("Failed to create lock attribute!\n"); return 1; } if (0 != pthread_rwlockattr_setpshared(&lkattr, PTHREAD_PROCESS_SHARED)) { printf("Failed to set lock attribute property!\n"); } else printf("Lock attribute property set successfully!\n"); for (j=0;j<2;j++) { if (0 != pthread_rwlock_init(&lk,attrs[j])) { printf("Failed to create lock!\n"); return 1; } if (0 == pthread_rwlock_unlock(&lk)) // This should fail { printf("Unlocking of an unlocked lock succeeded when it should not have.\n"); } if (j==0) printf("\nTrying 10 recursive RO locks with NULL (default) attributes ...\n",i); else printf("\nTrying 10 recursive RO locks with permissive custom attributes ...\n",i); for (i = 0; i < 10; i++) { if (0 != pthread_rwlock_rdlock(&lk)) { printf("Lock recursion iteration %d failed!\n",i); } else printf("Lock recursion iteration %d succeeded!\n",i); } for (i = 0; i < 10; i++) { if (0 != pthread_rwlock_unlock(&lk)) { printf("Unlock recursion iteration %d failed!\n",i); } else printf("Unlock recursion iteration %d succeeded!\n",i); } if (0 == pthread_rwlock_unlock(&lk)) // This should fail { printf("Unlocking of an unlocked lock succeeded when it should not have.\n"); } pthread_rwlock_destroy(&lk); } return 0; }