CLOCK_MONOTONIC和pthread_mutex_timedlock /那么pthread_cond_timedwait [英] CLOCK_MONOTONIC and pthread_mutex_timedlock / pthread_cond_timedwait

查看:792
本文介绍了CLOCK_MONOTONIC和pthread_mutex_timedlock /那么pthread_cond_timedwait的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的<一个href=\"http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_timedlock.html\">pthread_mutex_timedlock文档说, abs_timeout 需要 CLOCK_REALTIME 。然而,我们都知道这是不恰当的定时特定的时间(由于系统时间调整)。

The pthread_mutex_timedlock documentation says that abs_timeout takes a CLOCK_REALTIME. However, we all know that it is inappropriate for timing a specific duration (due to system time adjustments).

有没有一种方法,使对 CLOCK_MONOTONIC 是便携式pthread的锁超时?同样的问题也有那么pthread_cond_timedwait。

Is there a way to make pthread lock timeout on CLOCK_MONOTONIC that is portable? The same goes with pthread_cond_timedwait.

推荐答案

说完看了看文件和 pthreads.h中,我不能找到一种方法,使<一个href=\"http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_timedlock.html\">pthread_mutex_timedlock使用 CLOCK_MONOTONIC ,所以我认为这不是(目前)成为可能。对于<一个href=\"http://pubs.opengroup.org/onlinepubs/009604599/functions/pthread_cond_wait.html\">pthread_cond_timedwait,但是,您可以用code是这样的:

Having looked at the documentation and pthread.h, I can't find a way to make pthread_mutex_timedlock use CLOCK_MONOTONIC so I assume that's not (currently) possible. For pthread_cond_timedwait, however, you can use code like this:

pthread_condattr_t attr;
pthread_cond_t cond;
/* ... */
pthread_condattr_init(&attr);
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
pthread_cond_init(&cond, &attr);

我省略了错误code检查清楚,但当然你应该做的。

I've omitted error code checking for clarity, but of course you should do that.

我认为 CLOCK_REALTIME 的使用,因为它总是可用的,而在原则上 CLOCK_MONOTONIC 是可选的。另外,我不知道是否设置绝对超时后,使系统调用得到由信号等中断,更容易恢复。

I assume that CLOCK_REALTIME is used because it's always available whereas in principle CLOCK_MONOTONIC is optional. Also, I wonder if setting absolute timeouts makes it easier to recover after system calls get interrupted by signals and the like.

不过,它似乎颇不一致,该时钟可以在某些情况下设置,而不是别人 - 有真应了 pthread_mutexattr_setclock(),但很可惜有没有似乎是一个。我猜你只希望有人不设置时钟!

However, it does seem quite inconsistent that the clock can be set in some cases and not others - there really should be a pthread_mutexattr_setclock(), but alas there does not seem to be one. I guess you'll just have to hope someone doesn't set the clock!

这篇关于CLOCK_MONOTONIC和pthread_mutex_timedlock /那么pthread_cond_timedwait的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆