利用slect和poll。课本的答案上poll第一个参数传了个指针,而实际上直接给个NULL就可以,因为后面的参数0的意思是前面结构体数组的长度为0;
- #include
- #include
- void sleep_us(unsigned int usec){
- struct timeval tv;
- tv.tv_usec = usec % 1000000;
- tv.tv_sec = usec / 1000000;
- select(0, NULL, NULL, NULL, &tv);
- }
- #include
- void sleep_poll(unsigned int usec){
- struct pollfd dump;
- int timeout;
- if((timeout = usec / 1000) <= 0){
- timeout = 1;
- }
- poll(NULL, 0, usec);
- }
- int main(){
- sleep_poll(2000);
- return 0;
- }
阅读(749) | 评论(0) | 转发(0) |