mercredi 20 mars 2019

Tight loop in `main` is blocking all other processing

My application has a tight loop waiting for a special event, the loop checks over and over again for an event to happen...

int main()
{
  while(1)
  {
    // call a very quick function, (~1ms)
    if( !Ping() )
    {
      break;
    }
  }
}

The problem is that this tight loop seems to block any other work from happening.

Even if I wait and yield, it seems to still be blocking

...
std::this_thread::yield();
std::this_thread::sleep_for(std::chrono::milliseconds(5));
...

This is more obvious, (but not limited), with Windows created. Their message pump seems to be blocked.

Yes, I could call PeekMessage, but that's just for Windows, (and my app is not always a windows appp anyway), so I would rather use something to Yield CPU time to other threads.

while (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE) > 0)
{
  ...  
}

What would be the non MS Windows equivalent of PeekMessage?

How can I keep my tight loop, (I want to remain in the main thread), while surrendering CPU work to any other working threads.

Aucun commentaire:

Enregistrer un commentaire