当前位置: 技术问答>linux和unix
linux多线程编程中如何间隔100ms发送一次网络数据包,100ms的时间如何把握?
来源: 互联网 发布时间:2015-06-08
本文导语: RT | #include // 最小精度10ms void ms_sleep(unsigned long ms) { struct timeval tv; tv.sec = ms / 1000; tv.usec = (ms % 1000) * 1000; while ((-1 == select(0, 0, 0, 0, &tv)) && (EINTR == e...
RT
|
#include
// 最小精度10ms
void ms_sleep(unsigned long ms)
{
struct timeval tv;
tv.sec = ms / 1000;
tv.usec = (ms % 1000) * 1000;
while ((-1 == select(0, 0, 0, 0, &tv)) && (EINTR == errno);
}
call ms_sleep(100);
// 最小精度10ms
void ms_sleep(unsigned long ms)
{
struct timeval tv;
tv.sec = ms / 1000;
tv.usec = (ms % 1000) * 1000;
while ((-1 == select(0, 0, 0, 0, &tv)) && (EINTR == errno);
}
call ms_sleep(100);
|
#include
#include
#include
void Timer(int sec, long usec)
{
struct timeval tvSelect;
tvSelect.tv_sec = sec;
tvSelect.tv_usec = usec;
select(FD_SETSIZE, NULL, NULL, NULL, &tvSelect);
};
int main()
{
printf("--- begin ---n");
Timer(3, 1000*500);
printf("--- bye ---n");
}
#include
#include
void Timer(int sec, long usec)
{
struct timeval tvSelect;
tvSelect.tv_sec = sec;
tvSelect.tv_usec = usec;
select(FD_SETSIZE, NULL, NULL, NULL, &tvSelect);
};
int main()
{
printf("--- begin ---n");
Timer(3, 1000*500);
printf("--- bye ---n");
}
|
用计时器。
|
sleep(100)