是usleep()函式用C语言实现的忙等待? [英] Is usleep() in C implemented as busy wait?

查看:622
本文介绍了是usleep()函式用C语言实现的忙等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在建设有 pthreads的多线程应用程序,需要一个线程来定期检查一些东西。在中之间的这种线程不应该使用任何CPU时间。这可能与 usleep()函式?为 usleep()函式不是忙等待?还是有更好的解决方案?


解决方案

功能 usleep 已从SUSv4删除

。你应该使用了nanosleep 代替或计时器( setitimer函数等)。

对于R ..笔记的意见,应该在睡觉被实现为一个忙等待:


  • 的线程将继续使用CPU

  • 其他(低优先级)的线程将不会有机会运行

因此​​:


  • 也许有人会用信号(我认为SUSv3提到SIGALARM?)

  • 有些人可能会使用花式计时器

I'm building a multithreaded application with pthreads and need a thread to periodically check some stuff. During the time in between this thread shouldn't use any CPU. Is this possible with usleep()? Is usleep() not busy waiting? Or is there a better solution?

解决方案

The function usleep has been removed from SUSv4. You should probably use nanosleep instead or timers (setitimer etc).

As R.. notes in the comments, should the sleep be implemented as a busy wait:

  • The thread would continue to use the CPU
  • Other (lower-priority) threads wouldn't get a chance to run

Thus:

  • Some might use signals (I think SUSv3 mentioned SIGALARM ?)
  • Some might use fancy timers

这篇关于是usleep()函式用C语言实现的忙等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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