1 /** Hold several types of synchronization objects locked as long as specified.
14 static void delay_ms(const int ms
)
19 ts
.tv_sec
= ms
/ 1000;
20 ts
.tv_nsec
= (ms
% 1000) * 1000 * 1000;
24 int main(int argc
, char** argv
)
28 pthread_mutex_t mutex
;
29 pthread_mutexattr_t mutexattr
;
30 pthread_rwlock_t rwlock
;
32 while ((optchar
= getopt(argc
, argv
, "i:")) != EOF
)
37 interval
= atoi(optarg
);
40 fprintf(stderr
, "Usage: %s [-i <interval time in ms>].\n", argv
[0]);
45 fprintf(stderr
, "Locking mutex ...\n");
47 pthread_mutexattr_init(&mutexattr
);
48 pthread_mutexattr_settype(&mutexattr
, PTHREAD_MUTEX_RECURSIVE
);
49 pthread_mutex_init(&mutex
, &mutexattr
);
50 pthread_mutexattr_destroy(&mutexattr
);
51 pthread_mutex_lock(&mutex
);
53 pthread_mutex_lock(&mutex
);
54 pthread_mutex_unlock(&mutex
);
55 pthread_mutex_unlock(&mutex
);
56 pthread_mutex_destroy(&mutex
);
58 fprintf(stderr
, "Locking rwlock exclusively ...\n");
60 pthread_rwlock_init(&rwlock
, 0);
61 pthread_rwlock_wrlock(&rwlock
);
63 pthread_rwlock_unlock(&rwlock
);
64 pthread_rwlock_destroy(&rwlock
);
66 fprintf(stderr
, "Locking rwlock shared ...\n");
68 pthread_rwlock_init(&rwlock
, 0);
69 pthread_rwlock_rdlock(&rwlock
);
71 pthread_rwlock_rdlock(&rwlock
);
72 pthread_rwlock_unlock(&rwlock
);
73 pthread_rwlock_unlock(&rwlock
);
74 pthread_rwlock_destroy(&rwlock
);
76 fprintf(stderr
, "Done.\n");