本文整理汇总了C++中Timer::AddTimer方法的典型用法代码示例。如果您正苦于以下问题:C++ Timer::AddTimer方法的具体用法?C++ Timer::AddTimer怎么用?C++ Timer::AddTimer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Timer
的用法示例。
在下文中一共展示了Timer::AddTimer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TimerTestEntry
int TimerTestEntry(int argc, char* argv[])
{
Timer* timer = new ListTimer();
srand(time(NULL));
int i = 0;
int num = atoi(argv[1]);
g_pCounter = new int[num];
while (i++ < num)
{
int interval = 20 + rand() % (3600 * 1000 - 20);
int iTimerId = timer->AddTimer(interval, OnTimer, true);
printf("Timer-%d start, every %d msecond.\n", iTimerId, interval);
}
while (true)
{
zues::Sleep(1000000);
}
int nCount = atoi(argv[2]);
int iDelayUSec[] = {
500 * 1000,
100 * 1000,
50 * 1000,
10 * 1000,
5 * 1000, //select会10ms返回
2 * 1000,
//不再准确
2 * 1000,
1 * 1000,
100,
10,
1
};
int& nDelay = iDelayUSec[atoi(argv[1])];
BEGIN_COST_CALCULATE("select");
struct timeval delay;
while (i < nCount)
{
delay.tv_sec = 0;
delay.tv_usec = nDelay;
select(0, NULL, NULL, NULL, &delay);
i++;
}
END_COST_CALCULATE();
BEGIN_COST_CALCULATE("usleep");
i= 0;
while (i < nCount)
{
usleep(nDelay);
i++;
}
END_COST_CALCULATE();
/*
BEGIN_COST_CALCULATE("sleep");
i= 0;
while (i < 10)
{
sleep(1);
i++;
}
END_COST_CALCULATE();
*/
BEGIN_COST_CALCULATE("nanosleep");
i= 0;
struct timespec req;
struct timespec rem;
rem.tv_sec = 0;
rem.tv_nsec = 0;
req.tv_sec = 0;
req.tv_nsec = nDelay * 1000;
while (i < nCount)
{
if (-1 == nanosleep(&req, &rem))
{
printf("%d, %d\n", rem.tv_sec, rem.tv_nsec);
req.tv_sec = rem.tv_sec;
req.tv_nsec = nDelay * 1000 + rem.tv_nsec;
}
else
{
req.tv_sec = 0;
req.tv_nsec = nDelay * 1000;
}
i++;
}
END_COST_CALCULATE();
}