发现使用clock_nanosleep函数产生2ms的睡眠时间有困难
谢谢...我正在使用 posix precision sleep 标准库函数来睡眠,但它似乎返回得太快了。请检查此代码并提供修复该功能以正确睡眠正确时间的方法的详细信息。我需要至少 2 毫秒的睡眠时间。
void CModbusSlave::sleep(uint32_t seconds, uint32_t useconds)//arguments passed int32_t seconds=0,uint32_t useconds=2000ULL//
{
int timerResult = 0;
clock_gettime(CLOCK_MONOTONIC, &stopTime);
uint64_t absTime = (((static_cast<uint64_t>(stopTime.tv_sec) * 1000000000ULL) +
(static_cast<uint64_t>(stopTime.tv_nsec))) +
((static_cast<uint64_t>(seconds) * 1000000000ULL) +
(static_cast<uint64_t>(useconds)*1000ULL)));
stopTime.tv_sec = static_cast<uint32_t>(absTime / 1000000000ULL);
stopTime.tv_nsec = static_cast<uint32_t>(absTime % 1000000000ULL);
do
{
timerResult = clock_nanosleep(CLOCK_MONOTONIC,
TIMER_ABSTIME,
&stopTime,
&stopTime);
sched_yield();
}
while ((timerResult == -1) && (errno == EINTR));
}
Thanks... I’m using the posix precision sleep standard lib function to sleep, but it appears to be returning far too quickly. Please examine this code and provide details of a way which would repair the function to correctly sleep the correct amount of time.I need a sleep time of atleast 2ms..
void CModbusSlave::sleep(uint32_t seconds, uint32_t useconds)//arguments passed int32_t seconds=0,uint32_t useconds=2000ULL//
{
int timerResult = 0;
clock_gettime(CLOCK_MONOTONIC, &stopTime);
uint64_t absTime = (((static_cast<uint64_t>(stopTime.tv_sec) * 1000000000ULL) +
(static_cast<uint64_t>(stopTime.tv_nsec))) +
((static_cast<uint64_t>(seconds) * 1000000000ULL) +
(static_cast<uint64_t>(useconds)*1000ULL)));
stopTime.tv_sec = static_cast<uint32_t>(absTime / 1000000000ULL);
stopTime.tv_nsec = static_cast<uint32_t>(absTime % 1000000000ULL);
do
{
timerResult = clock_nanosleep(CLOCK_MONOTONIC,
TIMER_ABSTIME,
&stopTime,
&stopTime);
sched_yield();
}
while ((timerResult == -1) && (errno == EINTR));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论