pthread_tryjoin_np函数用法详解
pthread_tryjoin_np函数简介
- 头文件包含
#include <pthread.h>
- 函数定义
int pthread_tryjoin_np(pthread_t thread , void ** retval );
int pthread_timedjoin_np(pthread_t thread , void ** retval ,
const struct timespec * abstime );
- 编译链接选项
-pthread
pthread_tryjoin_np函数常见使用错误
- 链接错误
undefined reference to `pthread_tryjoin_np'
解决办法:添加链接选项
-pthread
- 编译错误
warning: implicit declaration of function ‘pthread_tryjoin_np’ [-Wimplicit-function-declaration]
解决办法:包含头文件
#include <pthread.h>
pthread_tryjoin_np函数详细描述
除了本页所述的不同之外,这些函数的操作方式与pthread_join (3)相同。
pthread_tryjoin_np ()函数执行一个非阻塞连接,如果thread尚未终止,线程thread 返回*retval 中线程的退出状态,那么调用将返回一个错误,而不是阻塞,就像pthread_join (3)所做的那样。
pthread_timedjoin_np ()函数执行带超时的联接。如果thread尚未终止,则呼叫将阻塞到abstime 中根据CLOCK_REALTIME时钟测量的最大时间。如果超时在thread终止之前过期,则调用返回错误。abstime参数是以下形式的结构,指定了自纪元以来测量的绝对时间(请参见time (2)):
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
pthread_tryjoin_np函数返回值
如果成功,这些函数返回0;出错时,它们返回一个错误号。
pthread_tryjoin_np函数错误码
这些函数可能会以与pthread_join (3)相同的错误失败,pthread_tryjoin_np ()也会以以下错误失败:
- EBUSY thread在呼叫时尚未终止。
此外,pthread_timedjoin_np ()可能会失败,并出现以下错误:
- ETIMEDOUT呼叫在thread终止之前超时。
- EINVAL abstime值无效。RI(tv_sec小于0或tv_nsec大于1E9)。
pthread_timedjoin_np ()从不返回错误EINTR
pthread_tryjoin_np函数使用举例
下面的代码等待加入最多5秒:
struct timespec ts;
int s;
\&...
if (clock_gettime(CLOCK_REALTIME, &ts) == \-1) {
/* Handle error */
}
ts.tv_sec += 5;
s = pthread_timedjoin_np(thread, NULL, &ts);
if (s != 0) {
/* Handle error */
}
评论区