sleep()是怎么实现的

cl002 2004-01-19 10:44:29
要将线程进行微妙几级挂起,现在是这样干的:
QueryPerformanceCounter(&litmp);
QPart1 = litmp.QuadPart;

while(ulElapse < m_ulSamCycle)
{
EnterCriticalSection(&m_cs);
QueryPerformanceCounter(&litmp);
QPart2 = litmp.QuadPart;

dfMinus = (double)(QPart2 - QPart1);
dfTim = dfMinus/dfFreq;
dfTimMir = SECTOMIR(dfTim);
ulElapse += (long) dfTimMir;
LeaveCriticalSection(&m_cs);
}
实在是太费系统资源,cpu利用率经常是100%,有没有想sleep样的函数可以实现微秒级的挂起?或者可不可以重写sleep()函数?大家给点建议
...全文
86 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZhangYang1981 2004-01-20
  • 打赏
  • 举报
回复
我也发现了同样的问题。
DuMiYue 2004-01-20
  • 打赏
  • 举报
回复

//Thread Waiting Function
int WaitWithMessageLoop(DWORD dwMilliseconds)
{
CEvent event;
bool bContinue = TRUE;
int iReturnVal = STATUS_WAIT_0;
while(bContinue)
{
switch(iReturnVal = ::MsgWaitForMultipleObjects(1, &event.GetHandle(), FALSE, dwMilliseconds, QS_ALLEVENTS | QS_ALLINPUT))
{
case WAIT_OBJECT_0:
bContinue = FALSE;
//::ResetEvent(pHandles[0]);
break;

case (WAIT_OBJECT_0 + 1):
MSG msg;
while(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
if(msg.message == WM_QUIT)
{
bContinue = FALSE;
break;
}
}
break;

case WAIT_TIMEOUT:
bContinue = FALSE;
break;

default:
;
}
}

return iReturnVal;
}
zhangcrony 2004-01-20
  • 打赏
  • 举报
回复
Pay attention to this: you'd better set up a timer!
hell190109 2004-01-19
  • 打赏
  • 举报
回复
毫秒级

15,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧