CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Sleep(0)

Threaded View

  1. #1
    Join Date
    Jul 2009
    Posts
    154

    Sleep(0)

    Hello,

    I have the following scenario:

    Multi-Threaded Server Application.
    I have a function which PeekMessage() all messages and returns if none left.
    I have a function that must be called every 12,5 ms.

    pseudo code:
    Code:
    bool PumpWindowMessages()
    {
      MSG msg;
      while( PeekMessage( &msg ) ) 
      {
         if( msg.message == WM_QUIT )
            return false;
    
         TranslateMessage( &msg );
         Dispatchmessage( &msg );
      }
    
      return true;
    }
    
    void TheInfiniteLoop()
    {
        LARGE_INTEGER liFreq, liStart, liStop;
        float fTime = 0.0f
    
        QueryPerformanceFrequency( &liFreq );
        while( PumpWindowMessages() )
        {
            QueryPerformanceCounter( &liStart );
    
            Update( fTime );
      
            QueryPerformanceCounter( &liStop );
    
            fTime += (float)(((liStop - liStart) * 1000000) / liFreq);
    
            Sleep( 0 );
        }
    }
    
    void Update( float fTime )
    {
      static float fTickTime = 0.0f;
    
      fTickTime += fTime;
    
      while( fTickTime >= 12.5f )
      {
        doMyFunctions()
        
        fTickTime -= 12.5f;
      }
    }
    
    void doMyFunctions()
    {
        EnterCriticalSection( &mainCritSec );
    
        CheckEverything();
    
        LeaveCriticalSection( &mainCritSec );
    }
    What do you think of this? Could it be done better? Since CPU load could be high due to the infinite loop, though the Sleep( 0 ) should reduce this.

    Some extra notes:
    Every incoming packet is received on another thread, but then with SendMessage() sent to the WndProc (WM_PACKET), so packets are handled in the main thread of the application.
    Last edited by ProgrammerC++; September 29th, 2010 at 08:40 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured