Rate This Document
Findability
Accuracy
Completeness
Readability

HTL_thread_cond_timedwait

Waits for the condition variable signal and sets the timeout period.

Interface Definition

int HTL_thread_cond_timedwait(HTL_thread_cond_t *thread_cond, HTL_thread_mutex_t *thread_mutex, const struct timespec *time);

Description

The caller of HTL_thread_cond_timedwait() waits for the condition variable thread_cond to be sent within the absolute time specified by time. Users must call this function when the mutex thread_mutex is locked. The mutex is automatically released when waiting. When the caller is woken up, the mutex is automatically locked by the caller. Users need to unlock the mutex later. If the system time exceeds the specified time before the conditional signal is sent, the error code HTL_THREAD_ERR_COND_TIMEDOUT is returned.

This function associates thread_mutex with thread_cond. Users cannot associate multiple mutexes for the same condition variable.

Even if an error occurs, the mutex is still locked after this function returns a value.

If the mutex is recursive, the caller can lock the mutex only once.

Parameters

Parameter

Type

Description

Input/Output

thread_cond

HTL_thread_cond_t *

Address of the handle to the condition variable.

Input

thread_mutex

HTL_thread_mutex_t *

Address of the handle to the mutex.

Input

time

struct timespec*

Absolute timeout period.

Input

Return Value

  • HTL_THREAD_SUCCESS: success.
  • Other values: failure. For details, see the error code descriptions.

Example

    HTL_thread_cond_t cond = HTL_THREAD_COND_NULL;
    HTL_thread_mutex_t mutex = HTL_THREAD_MUTEX_NULL;

    struct timespec ts;
    struct timeval tv;
    ret = gettimeofday(&tv, NULL);
    assert(!ret);

    ts.tv_sec = tv.tv_sec;
    ts.tv_nsec = tv.tv_usec * 1000;
    ts.tv_sec += 1;
    HTL_thread_cond_timedwait(&cond, &mutex, &ts);