Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.4a |
both | POSIX(ON) |
#define _OPEN_THREADS
#include <pthread.h>
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
const struct timespec *abstime);
#define _UNIX03_THREADS
#include <pthread.h>
int pthread_cond_timedwait(pthread_cond_t * __restrict__cond,
pthread_mutex_t * __retrict__mutex,
const struct timespec * __restrict__abstime);
When such timeouts occur, pthread_cond_timedwait() reacquires the mutex, referenced by mutex (created by pthread_mutex_init()).
If successful, pthread_cond_timedwait() returns 0.
If unsuccessful, pthread_cond_timedwait() returns -1 and sets errno to one of the following values:
For a shared condition variable, the time specified by abstime has passed.
Special behavior for Single UNIX Specification, Version 3: If unsuccessful, pthread_cond_timedwait() returns an error number to indicate the error.
If the condition variable is shared (PTHREAD_PROCESS_SHARED), the mutex must also be shared, with the _OPEN_SYS_MUTEX_EXT feature defined when the mutex was created and initialized.
If the condition variable is private (PTHREAD_PROCESS_PRIVATE), the mutex must also be private.
If the condition variable is shared, all calls to pthread_cond_wait() or pthread_cont_timedwait() for a given condition variable must use the same mutex for the life of the process, or until both the condition variable and mutex are destroyed (using pthread_cond-destroy() and pthread_mutex_destroy()).
⁄* CELEBP21 *⁄
#define _OPEN_THREADS
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
main() {
pthread_cond_t cond;
pthread_mutex_t mutex;
time_t T;
struct timespec t;
if (pthread_mutex_init(&mutex, NULL) != 0) {
perror("pthread_mutex_init() error");
exit(1);
}
if (pthread_cond_init(&cond, NULL) != 0) {
perror("pthread_cond_init() error");
exit(2);
}
if (pthread_mutex_lock(&mutex) != 0) {
perror("pthread_mutex_lock() error");
exit(3);
}
time(&T);
t.tv_sec = T + 2;
printf("starting timedwait at %s", ctime(&T));
if (pthread_cond_timedwait(&cond, &mutex, &t) != 0)
if (errno == EAGAIN)
puts("wait timed out");
else {
perror("pthread_cond_timedwait() error");
exit(4);
}
time(&T);
printf("timedwait over at %s", ctime(&T));
}
starting timedwait at Fri Jun 16 10:44:00 2001
wait timed out
timedwait over at Fri Jun 16 10:44:02 2001