当前位置: 技术问答>linux和unix
linux系统下usleep()函数使用的问题?
来源: 互联网 发布时间:2017-01-15
本文导语: 各位,我在linux下开发一应用程序,其中一段的程序大概如下: while(1) { FuncDoThing(); usleep(secs*1000*1000); //secs为暂停的秒数.secs的值大概为1,2,3. if(满足某条件) break; } 这段程序刚开始时,运行时...
各位,我在linux下开发一应用程序,其中一段的程序大概如下:
while(1)
{
FuncDoThing();
usleep(secs*1000*1000); //secs为暂停的秒数.secs的值大概为1,2,3.
if(满足某条件)
break;
}
这段程序刚开始时,运行时每隔几秒会运行FuncDoThing函数,
一切很正常。但是当发生一次usb设备断开连接后,
感觉就不停的去运行FuncDoThing()函数,没有任何延迟,
usleep()没有起到作用,所以想问一下各位,usleep()函数使用有什么技巧.谢谢!
while(1)
{
FuncDoThing();
usleep(secs*1000*1000); //secs为暂停的秒数.secs的值大概为1,2,3.
if(满足某条件)
break;
}
这段程序刚开始时,运行时每隔几秒会运行FuncDoThing函数,
一切很正常。但是当发生一次usb设备断开连接后,
感觉就不停的去运行FuncDoThing()函数,没有任何延迟,
usleep()没有起到作用,所以想问一下各位,usleep()函数使用有什么技巧.谢谢!
|
SYNOPSIS
#include
int pselect(int nfds, fd_set *restrict readfds,
fd_set *restrict writefds, fd_set *restrict errorfds,
const struct timespec *restrict timeout,
const sigset_t *restrict sigmask);
int select(int nfds, fd_set *restrict readfds,
fd_set *restrict writefds, fd_set *restrict errorfds,
struct timeval *restrict timeout);
void FD_CLR(int fd, fd_set *fdset);
int FD_ISSET(int fd, fd_set *fdset);
void FD_SET(int fd, fd_set *fdset);
void FD_ZERO(fd_set *fdset);
DESCRIPTION
The pselect() function shall examine the file descriptor sets whose addresses are passed in the readfds, writefds, and errorfds
parameters to see whether some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition
pending, respectively.
The select() function shall be equivalent to the pselect() function, except as follows:
* For the select() function, the timeout period is given in seconds and microseconds in an argument of type struct timeval,
whereas for the pselect() function the timeout period is given in seconds and nanoseconds in an argument of type struct time‐
spec.
用pselect替代把,提供nanoseconds级别的睡眠。
#include
int pselect(int nfds, fd_set *restrict readfds,
fd_set *restrict writefds, fd_set *restrict errorfds,
const struct timespec *restrict timeout,
const sigset_t *restrict sigmask);
int select(int nfds, fd_set *restrict readfds,
fd_set *restrict writefds, fd_set *restrict errorfds,
struct timeval *restrict timeout);
void FD_CLR(int fd, fd_set *fdset);
int FD_ISSET(int fd, fd_set *fdset);
void FD_SET(int fd, fd_set *fdset);
void FD_ZERO(fd_set *fdset);
DESCRIPTION
The pselect() function shall examine the file descriptor sets whose addresses are passed in the readfds, writefds, and errorfds
parameters to see whether some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition
pending, respectively.
The select() function shall be equivalent to the pselect() function, except as follows:
* For the select() function, the timeout period is given in seconds and microseconds in an argument of type struct timeval,
whereas for the pselect() function the timeout period is given in seconds and nanoseconds in an argument of type struct time‐
spec.
用pselect替代把,提供nanoseconds级别的睡眠。
|
会不会是因为usleep被某种信号中断了
|
/*sleep in us*/
void usSleep(unsigned int nusecs)
{
struct timeval tval;
tval.tv_sec = nusecs / 1000000;
tval.tv_usec = nusecs % 1000000;
select(0, NULL, NULL, NULL, &tval);
}
|
这个值得围观。
|
另外pselect可以睡眠时阻塞一个信号集,const sigset_t *restrict sigmask,可能正好也符号你的要求。
另外,我看有个ualarm函数:
SYNOPSIS
#include
useconds_t ualarm(useconds_t usecs, useconds_t interval);
提供微秒级的。
另外,我看有个ualarm函数:
SYNOPSIS
#include
useconds_t ualarm(useconds_t usecs, useconds_t interval);
提供微秒级的。
|
用select吧,避免不必要的干扰
|
好!!!
|
你换一个呗,用select也可以
|
+1
|
usleep()是个短延迟,应该使用select,它可以达到一个时钟中断的周期精度。
|
不懂。帮顶
|
关注下