首页 > 解决方案 > 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?

标签: linuxlinux-kernelposixsleep

解决方案


您的线程应该阻塞,而不是休眠,无论是在read()套接字上的调用中还是在多路复用调用中,例如select()or poll()

阻塞就像睡觉一样,只是它会在新数据到来时立即唤醒。


推荐阅读