Why window hangs if message loop is run asynchronously?

Viewed 52

Currently I am experimenting with performance of my program.

Initially my render loop was realized via Windows timer with minimum delay. This approach has not bad performance.

In all examples I see, including DirectX programs render loop is put inside message loop like this:

MSG msg = {};

while (WM_QUIT != msg.message)
{
    if (GetMessage(&msg, nullptr, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    
    RenderLoop();
}

And in my case this is worst solution, because when I run some timer for animations fps drops.


But, nevermind. So I thought, what if to put message loop into another thread, like this:

bool Exit = false;
void Loop()
{
   MSG msg = {};

    while (WM_QUIT != msg.message)
    {
        if (GetMessage(&msg, nullptr, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
}

int main()
{
       // Create window, all program stuff

       thread th(Loop);
       th.detach();

       while(!Exit)
             RenderLoop();

       return;
}

But the program is hanging (not responding) when starts. Which is interesting, because I actually can't straightly answer why it happens.

Yes, main thread is indeed hanged, but why program "does not respond"? It calls GetMessage/DispatchMessage, hence should signal Windows that it is responding.

0 Answers
Related