Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why doesn't message pump/loop block all threads?

Why doesn't message pump/loop block all threads?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sawerr
    wrote on last edited by
    #1

    Hi I have a basic question about classic message loop of Win32 programs.

    while (GetMessage(&msg, NULL, 0, 0))
    {
    	if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    }
    

    Why doesn't this message loop take all processor time? I also increment the priority level:

    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);

    In another MFC application i created a new thread with infinite loop( I mean in function there is only "infinite while loop", as classic win32 message loop) and also set priority to THREAD_PRIORITY_HIGHEST. That loop blocked all other threads. But why doesn't win32 message loop block other threads? Thanks.

    M 1 Reply Last reply
    0
    • S sawerr

      Hi I have a basic question about classic message loop of Win32 programs.

      while (GetMessage(&msg, NULL, 0, 0))
      {
      	if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
      	{
      		TranslateMessage(&msg);
      		DispatchMessage(&msg);
      	}
      }
      

      Why doesn't this message loop take all processor time? I also increment the priority level:

      SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);

      In another MFC application i created a new thread with infinite loop( I mean in function there is only "infinite while loop", as classic win32 message loop) and also set priority to THREAD_PRIORITY_HIGHEST. That loop blocked all other threads. But why doesn't win32 message loop block other threads? Thanks.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      sawerr wrote:

      Why doesn't this message loop take all processor time?

      Because GetMessage() blocks until a message is available in the queue. If you want it to spin and consume nearly all of the CPU time for a core, use PeekMessage() instead of GetMessage(). Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        sawerr wrote:

        Why doesn't this message loop take all processor time?

        Because GetMessage() blocks until a message is available in the queue. If you want it to spin and consume nearly all of the CPU time for a core, use PeekMessage() instead of GetMessage(). Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        S Offline
        S Offline
        sawerr
        wrote on last edited by
        #3

        Mark Salsbery wrote:

        Because GetMessage() blocks until a message is available in the queue.

        Is this something like that: GetMessage() { while(true) { if(queue == EMPTY) Sleep(100); } }

        L M 2 Replies Last reply
        0
        • S sawerr

          Mark Salsbery wrote:

          Because GetMessage() blocks until a message is available in the queue.

          Is this something like that: GetMessage() { while(true) { if(queue == EMPTY) Sleep(100); } }

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          We all would hope the Windows kernel is event driven, and not polling all the time. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Voting for dummies? No thanks. X|


          S 1 Reply Last reply
          0
          • L Luc Pattyn

            We all would hope the Windows kernel is event driven, and not polling all the time. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Voting for dummies? No thanks. X|


            S Offline
            S Offline
            sawerr
            wrote on last edited by
            #5

            OK, but how can its pseudo-code be?

            L 1 Reply Last reply
            0
            • S sawerr

              Mark Salsbery wrote:

              Because GetMessage() blocks until a message is available in the queue.

              Is this something like that: GetMessage() { while(true) { if(queue == EMPTY) Sleep(100); } }

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              sawerr wrote:

              Is this something like that

              No, not at all. Within GetMessage(), there's something like

              GetMessage(...)
              {
              // wait for a new message to be queued
              WaitForSingleObject(QueuedMessageEvent)

              return message to caller
              

              }

              If the event isn't signalled when GetMessage() is called, WaitForSingleObject() puts the thread in a "wait state", a state where the thread is almost completely suspended - it uses VERY little CPU time. Using Sleep as you've shown is extremely inefficient. :) Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              S 1 Reply Last reply
              0
              • S sawerr

                OK, but how can its pseudo-code be?

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi,

                GetMessage() {
                if(queue == EMPTY) {
                queue.AddWaiter(currentThread);
                SwitchToForemostReadyThread();
                }
                return queue.RemoveMsg();
                }

                PutMessage(Message msg) {
                queue.AddMsg(msg);
                if(queue.HasWaitersWithHigherPriority(myPriority)) {
                Thread thread=queue.RemoveWaiter();
                SwitchToThread(thread);
                }
                }

                Not a single busy-wait or polling loop! The heart in the matter is the SwitchTo...Thread() methods save the current thread state on its stack, and load another thread's state from its stack, effectively performing a "thread switch". :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Voting for dummies? No thanks. X|


                1 Reply Last reply
                0
                • M Mark Salsbery

                  sawerr wrote:

                  Is this something like that

                  No, not at all. Within GetMessage(), there's something like

                  GetMessage(...)
                  {
                  // wait for a new message to be queued
                  WaitForSingleObject(QueuedMessageEvent)

                  return message to caller
                  

                  }

                  If the event isn't signalled when GetMessage() is called, WaitForSingleObject() puts the thread in a "wait state", a state where the thread is almost completely suspended - it uses VERY little CPU time. Using Sleep as you've shown is extremely inefficient. :) Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  S Offline
                  S Offline
                  sawerr
                  wrote on last edited by
                  #8

                  Thanks for answers. :rose:

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups