Make the choice-based cycle as responsive as possible

This thread will be very sensitive to network activity, but it can be guaranteed to process the message queue only 100 times per second. I can continue to reduce latency, but after a certain moment I will be busy waiting and chewing on the processor. Is it true that this solution is as good as I get without switching to another method?

// semi pseudocode
while (1) {
  process_thread_message_queue(); // function returns near-instantly
  struct timeval t;
  t.tv_sec = 0;
  t.tv_usec = 10 * 1000; // 10ms = 0.01s
  if (select(n,&fdset,0,0,t)) // see if there are incoming packets for next 1/100 sec
  {
    ... // respond with more packets or processing
  }
}
+3
source share
1 answer

It depends on what your OS provides for you. On Windows, you can wait for a stream message and a bunch of descriptors at the same time using MsgWaitForMultipleObjectsEx. This solves your problem. On another OS, you should have something like this.

+2

All Articles