Re: 99% CPU usage in CWinThread::PumpMessage

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: KS (ks_at_blah.com)
Date: 04/12/04


Date: Mon, 12 Apr 2004 10:18:44 -0600

Here's the code from ThrdCore.cpp

BOOL CWinThread::PumpMessage()
{
 ASSERT_VALID(this);

 if (!::GetMessage(&m_msgCur, NULL, NULL, NULL))
 {
#ifdef _DEBUG
  if (afxTraceFlags & traceAppMsg)
   TRACE0("CWinThread::PumpMessage - Received WM_QUIT.\n");
  m_nDisablePumpCount++; // application must die
   // Note: prevents calling message loop things in 'ExitInstance'
   // will never be decremented
#endif
  return FALSE;
 }

#ifdef _DEBUG
 if (m_nDisablePumpCount != 0)
 {
  TRACE0("Error: CWinThread::PumpMessage called when not permitted.\n");
  ASSERT(FALSE);
 }
#endif

#ifdef _DEBUG
 if (afxTraceFlags & traceAppMsg)
  _AfxTraceMsg(_T("PumpMessage"), &m_msgCur);
#endif

 // process this message

 if (m_msgCur.message != WM_KICKIDLE && !PreTranslateMessage(&m_msgCur))
 {
  ::TranslateMessage(&m_msgCur);
  ::DispatchMessage(&m_msgCur);
 }
 return TRUE;
}

You can see it really doesn't do much, other than a ::TranslateMessage() and
::DispatchMessage() ...

The only suggestion I have is to subclass the CWinThread class with your own
class and override this method and put the same code into your method. Then
after the ::DispatchMessage() add a Sleep(10). You'd also be able to see
what messages are being passed that is hogging up all the CPU usage.

KS

"Olaf van der Spek" <Olaf@XCC.TMFWeb.NL> wrote in message
news:utEhUWKIEHA.3276@tk2msftngp13.phx.gbl...
> KS wrote:
> > Not sure without seeing the code.
>
> If I understand the profiler output correctly, the following lines tell
> me that the PumpMessage function consumed 98.6% of the time itself and
> 99.1% of the time was consumed by itself and it's children.
> >> 75351,273 98,6 75773,696 99,1 1437
> >>CWinThread::PumpMessage(void) (mfc42d.dll)
>
> > But if you have a thread that has a loop in it, I'd suggest putting a
> > Sleep(100) inside the loop. I've had problems with threads hogging up
> > resources.
>
> I do have a loop, but it's blocking on a select call.
> >
> > Another possibility is to lower the thread's priority.
>
> That won't solve the CPU usage itself, although it's a reasonable
> temporary workaround.
> >
> > KS
> >
> > "Olaf van der Spek" <Olaf@XCC.TMFWeb.NL> wrote in message
> > news:u6f7utJIEHA.3444@TK2MSFTNGP11.phx.gbl...
> >
> >>Hi,
> >>
> >>I'm writing a file transfer application and I'm stuck with a nasty
> >>issue. For some weird reason, CPU usage hits 99% some times. So I fired
> >>up the VC6 profiler, the results are attached.
> >>
> >>98% of the time is spend inside CWinThread::PumpMessage
> >>But that's a simple function, how can so much time be spend there?
> >>
> >>Profile: Function timing, sorted by time
> >>Date: Sun Apr 11 21:12:13 2004
> >>
> >>
> >>Program Statistics
> >>------------------
> >> Command line at 2004 Apr 11 20:57: "F:\VC\xbt\XBT Client\Debug\XBT
> >>Client"
> >> Total time: 76459,318 millisecond
> >> Time outside of functions: 20,774 millisecond
> >> Call depth: 36
> >> Total functions: 3188
> >> Total hits: 1435373
> >> Function coverage: 39,6%
> >> Overhead Calculated 2
> >> Overhead Average 2
> >>
> >>Module Statistics for xbt client.exe
> >>------------------------------------
> >> Time in module: 76438,544 millisecond
> >> Percent of time in module: 100,0%
> >> Functions in module: 3188
> >> Hits in module: 1435373
> >> Module function coverage: 39,6%
> >>
> >> Func Func+Child Hit
> >> Time % Time % Count Function
> >>---------------------------------------------------------
> >> 75351,273 98,6 75773,696 99,1 1437
> >>CWinThread::PumpMessage(void) (mfc42d.dll)
> >> 75,806 0,1 75,806 0,1 58546 operator delete(void *)
> >>(mfc42d.dll)
> >> 73,318 0,1 107,126 0,1 155186
> >>std::allocator<bool>::destroy(bool *) (bt_file.obj)
> >
> >
> >



Relevant Pages

  • [ifort] Opening too much time files cause a segmentation fault
    ... My program consist in a general loop that first perform a kind of bisection and then write the results in multiple files. ... The problem doesn't seems to be in my code but when opening the files. ...
    (comp.lang.fortran)
  • Re: question about thread scheduling
    ... I will try what you suggested, the reason that I didn't use the sleep method ... If the NN run in a different thread as the control loop ... Sleepputs your thread to sleep for 3 timer ticks and ...
    (microsoft.public.windowsce.platbuilder)
  • Re: [ifort] Opening too much time files cause a segmentation fault
    ... My program consist in a general loop that first perform a kind of bisection and then write the results in multiple files. ... The problem doesn't seems to be in my code but when opening the files. ...
    (comp.lang.fortran)
  • Re: netbsd vulnerabilities
    ... > The attached code fixes the semop bug which is specified in the recent ... this change introduces an infinite loop bug. ... #ifdef SEM_DEBUG ...
    (FreeBSD-Security)
  • Re: while loop
    ... so I don't see why the while loop is causing this error ... time debugging in the long run if you make your code easier to read. ... > ENDIF ... Catherine Rees Lay ...
    (comp.lang.fortran)