大家帮忙看看,为什么Sleep(1), 结果确是15,16了
//大家帮忙看看
while(siStart == 1)
{
while(pOutArr->ucMute == 1) Sleep(1);
pOutArr->ucMute = 1;
pFrame = pOutArr->Get();
if(pFrame != NULL)
{
Video_SendDecodeMessage(slDecodeThread, (DWORD)pFrame->buf, pFrame->uwLen);
udw = pFrame->udwGobno;
pOutArr->ucMute = 0;
//*******************************************************//
DWORD udw1 = GetTickCount();
Sleep(1);
Trace("time: %u\n", GetTickCount() - udw1);
//*******************************************************//
//这个地方是我测试用的,每次都是输出time: 15 或者 time: 16
//为什么啊, 我只让它Sleep(1)****************************//
}
else
{
pOutArr->ucMute = 0;
Sleep(1);
}
}
问题点数:20、回复次数:15Top
1 楼snlux(snlux)回复于 2006-03-04 00:54:17 得分 0
GetTickCount计数不准确,看一下MSDN里的说明
Remarks
The following table describes the resolution of the system timer.
System Resolution
Windows NT 3.5 and later The system timer runs at approximately 10ms.
Windows NT 3.1 The system timer runs at approximately 16ms.
Windows 95 and later The system timer runs at approximately 55ms
Top
2 楼babam()回复于 2006-03-04 09:23:20 得分 0
sleep其实不准,如果cpu忙的话Top
3 楼zephyr007(道可道)回复于 2006-03-04 09:44:28 得分 0
Sleep()根本无法精确到1msTop
4 楼vcmute(BCare4 H1Rest Good9!)回复于 2006-03-04 09:49:08 得分 0
用WaitabeTimer
HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;
liDueTime.QuadPart=-100000000;
// Create a waitable timer.
hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
if (!hTimer)
{
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
return 1;
}
printf("Waiting for 10 seconds...\n");
// Set a timer to wait for 10 seconds.
if (!SetWaitableTimer(
hTimer, &liDueTime, 0, NULL, NULL, 0))
{
printf("SetWaitableTimer failed (%d)\n", GetLastError());
return 2;
}
// Wait for the timer.
if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());
else printf("Timer was signaled.\n");
return 0;
Top
5 楼stevecrisewu(月亮骑士)回复于 2006-03-04 09:52:05 得分 0
windows时间精度大概在50ms左右
因此没有一种办法可以确保这个时间精度Top
6 楼deutsch(人民)回复于 2006-03-04 12:55:32 得分 0
楼上说的有理,Sleep和GetTickCount对于10ms以下的延迟误差很高,应该用QueryPerformanceCounter实现在比较对Top
7 楼oyljerry(【勇敢的心】→ ㊣提拉米苏√㊣)回复于 2006-03-04 14:45:23 得分 0
Sleep精度不够~Top
8 楼shengoo(马儿)回复于 2006-03-04 15:12:53 得分 20
我测试过了,现在windows的精度就是15ms左右的,看系统的繁忙程度,这个再windows编程一书中有说明的,要准确的,请用多媒体定时器,那个是精确的,要非常精确就需要一些技巧了!Top
9 楼JetLuo(JetLuo)回复于 2006-03-04 20:17:27 得分 0
Sleep()的精度大约为20ms,Sleep(1)是无法实现的,要用如此高的精度,必须用多媒体定时器Top
10 楼zbplusplus(于是开始苏鲁之的堕落)回复于 2006-03-04 20:49:43 得分 0
Sleep导致进程睡眠,下次被唤醒经过了15或16毫秒
由于调度机制限制,不可能让一个进程休眠1毫秒Top
11 楼zyyoung(倡导开源)回复于 2006-03-05 15:12:27 得分 0
学习Top
12 楼angel_rabbit(zj_rabbit)回复于 2006-03-05 16:26:10 得分 0
mark,,,,Top
13 楼klkkklkl(哈哈)回复于 2006-03-05 17:22:46 得分 0
sleep(1)是不能精确到 1毫秒的
在多线程里会用sleep(1)来让出cup时间片
Top
14 楼unionize(同盟会)回复于 2006-03-06 14:56:23 得分 0
大概能精确到20毫秒Top
15 楼gohappy_1999(碧水蓝天)回复于 2006-03-06 15:41:03 得分 0
Sleep()会引起windows重新调度,所以实际至少会延时一个CPU时间片(大概15ms)Top




