pthread_mutex_timedlock, pthread_mutex_reltimedlock_np - lock a mutex
cc –mt [ flag... ] file... [ library... ] #include <pthread.h> #include <time.h> int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex, const struct timespec *restrict abs_timeout);
int pthread_mutex_reltimedlock_np(pthread_mutex_t *restrict mutex, const struct timespec *restrict rel_timeout);
The pthread_mutex_timedlock() function locks the mutex object referenced by mutex. If the mutex is already locked, the calling thread blocks until the mutex becomes available as in the pthread_mutex_lock(3C). If the mutex cannot be locked without waiting for another thread to unlock the mutex, this wait is terminated when the specified timeout expires.
The pthread_mutex_reltimedlock_np() function is identical to the pthread_mutex_timedlock() function, except that the timeout is specified as a relative time interval.
For pthread_mutex_timedlock(), the timeout expires when the absolute time specified by abs_timeout passes, as measured by the clock on which timeouts are based (that is, when the value of that clock equals or exceeds abs_timeout), or if the absolute time specified by abs_timeout has already been passed at the time of the call.
For pthread_mutex_reltimedlock_np(), the timeout expires when the time interval specified by rel_timeout passes, as measured by the CLOCK_REALTIME clock, or if the time interval specified by rel_timeout is negative at the time of the call.
The resolution of the timeout is the resolution of the CLOCK_REALTIME clock. The timespec data type is defined in the time.hheader.
Under no circumstance will either function fail with a timeout if the mutex can be locked immediately. The validity of the timeout parameter is not checked if the mutex can be locked immediately.
As a consequence of the priority inheritance rules (for mutexes initialized with the PRIO_INHERIT protocol), if a timed mutex wait is terminated because its timeout expires, the priority of the owner of the mutex is adjusted as necessary to reflect the fact that this thread is no longer among the threads waiting for the mutex.
Upon successful completion, the pthread_mutex_timedlock() and pthread_mutex_reltimedlock_np() functions return 0. Otherwise, an error number is returned to indicate the error.
The pthread_mutex_timedlock() and pthread_mutex_reltimedlock_np() functions will fail for the same reasons as pthread_mutex_lock(3C). In addition, they will fail if:
The caller would have blocked and the timeout parameter specified a nanoseconds field value less than zero or greater than or equal to 1,000 million.
The mutex could not be locked before the specified timeout expired.
See attributes(5) for descriptions of the following attributes: