开发者

Make select based loop as responsive as possible

开发者 https://www.devze.com 2023-02-19 08:39 出处:网络
This thread will be very responsive to network activity but can be guaranteed to process the message queue only as often as 100 times a second. I can keep reducing the timeout but after a certain poin

This thread will be very responsive to network activity but can be guaranteed to process the message queue only as often as 100 times a second. I can keep reducing the timeout but after a certain point I will be busy-waiting and chewing up CPU. Is it true that this solution is about as good as I'll get with开发者_如何学运维out 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
  }
}


It depends on what your OS provides for your. On Windows you can wait for a thread message and a bunch of handles simultaneously using MsgWaitForMultipleObjectsEx. This solves your problem. On other OS you should have something similar.

0

精彩评论

暂无评论...
验证码 换一张
取 消