What's the most CPU-efficient way to “waste time” in a thread?

前端 未结 3 806
遥遥无期
遥遥无期 2021-02-20 06:08

I have a number of threads (100\'s) that each execute for a few seconds at a time. When they are executing, they spend a significant amount of that time waiting for a response

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-20 06:49

    I cannot speak for AsyncFree's capabilities, but in general COM port programming in Windows supports Overlapped I/O, so you can efficiently wait for a notification when data arrives by using the WaitCommEvent() function with one of the WaitFor...() family of functions, such as WaitForSingleObject(). The thread can be put into a sleep state until the notify is issues, at which time it "wakes up" to read from the port until there is nothing further to read, then it can go back to sleep until the next notify.

提交回复
热议问题