linux - Under what conditions should a program use POSIX sleep?
问题描述
I've written a simple TCP/IP client application that continuously checks for new data from a server. The thread that implements this loop is scheduled at sched_priority = 0
and sched_policy = SCHED_OTHER
. Essentially I've made it the lowest priority.
The server only sends new data once a second which means the continuous checking is somewhat unnecessary. However, I'm questioning whether or not I should use a POSIX sleep
or if the fact I've scheduled it at the lowest priority is good enough.
Under what conditions should a program contain a sleep
? Should I be concerned CPU over-usage in my application?
解决方案
您的线程应该阻塞,而不是休眠,无论是在read()
套接字上的调用中还是在多路复用调用中,例如select()
or poll()
。
阻塞就像睡觉一样,只是它会在新数据到来时立即唤醒。
推荐阅读
- javascript - React redux search filter not displaying filtered content?
- android - 在 Android Studio 中在 Android Pie 设备上构建应用程序失败
- node.js - 从IONIC传输Node JS API中的图像文件时将FormData设为Null
- r - rMap 塞尔维亚在哪里找?
- java - java.util.ConcurrentModificationException 流
- jpa - 在 OneToOne 关联中禁用将 2 个孩子保存到父母的可能性
- apache-camel - 在骆驼中每小时刷新一次属性
- alexa - 自定义 Alexa 技能不适用于回声点,但适用于模拟器
- html - “文本溢出:省略号”和“溢出:隐藏”在我的中无法正常工作,并出现水平滚动条
- c - 无操作 C 宏的“全局范围内的分号”警告的解决方法