pthread_timedjoin,pthread_cond_timedwait

前几天使用pthread_cond_timedwait和pthread_timedjoin_np始终超时,最后查到原因是使用clock_gettime不正确导致

pthread_timedjoin_np中的时间为绝对时间,而我使用的clock_gettime(CLOCK_MONOTONIC,&ts);取得的时间为相对时间,导致超时。

 

1.精确级别,纳秒级别

原型

int clock_gettime(clockid_t clk_id, struct timespect * tp);

clk_id参数解释

CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变

CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响

CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间

CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间

 

pthread_timedjoin()

Join a thread, with a time limit

Synopsis:

#include <pthread.h>

int pthread_timedjoin( 
       pthread_t thread,
       void** value_ptr,
       const struct timespec* abstime );

Arguments:

thread
The target thread whose termination you're waiting for.
value_ptr
NULL, or a pointer to a location where the function can store the value passed to pthread_exit() by the target thread.
abstime
A pointer to a timespec structure that specifies the maximum time to wait for the join, expressed as an absolute time.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The pthread_timedjoin() function is similar to pthread_join(), except that an error of ETIMEDOUT is returned if the join doesn't occur before the absolute time specified by abstime passes (i.e. the system time is greater than or equal to abstime):

If you are not too long, I will wait here for you all my life.
— Oscar Wilde, The Importance of Being Earnest

The pthread_timedjoin() function blocks the calling thread until the target thread thread terminates, unless thread has already terminated. If value_ptr is non-NULL and pthread_timedjoin() returns successfully, then the value passed to pthread_exit() by the target thread is placed in value_ptr. If the target thread has been canceled then value_ptr is set to PTHREAD_CANCELED.

The target thread must be joinable. Multiple pthread_join(), pthread_timedjoin(), ThreadJoin(), and ThreadJoin_r() calls on the same target thread aren't allowed. When pthread_timedjoin() returns successfully, the target thread has been terminated.

Returns:

EOK
Success.
EBUSY
The thread thread is already being joined.
EDEADLK
The thread thread is the calling thread.
EFAULT
A fault occurred trying to access the buffers provided.
EINVAL
The thread thread isn't joinable.
ESRCH
The thread thread doesn't exist.
ETIMEDOUT
The absolute time specified in abstime passed before the join occurred.

Classification:

QNX Neutrino

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

你可能感兴趣的:(JOIN,thread,function,struct,library,structure)