c++ - Winsock2 select() function: passing {0, 0} as timeout parameter -
i'm creating multiplayer real-time game based on client-server model using <winsock2.h>
library. communication part, i've decided use non-blocking sockets, instead of blocking sockets eliminate multi-threading. on client side, handle these tasks in 1 single loop:
- handling user input (if necessary)
- sending/receiving data to/from server (if possible)
- updating game data (with constant frequency)
- refreshing screen (with constant frequency)
i'm wondering, if bad practice call select
timeout {0, 0} in second part of loop? found this website says:
the timeout value {0, 0} indicates select() return immediately, allowing application poll on select() operation. this should avoided performance reasons.
i don't understand, why should avoid using it. if explain, appreciate it.
i don't understand, why should avoid [calling select() zero-timeout). if explain, appreciate it.
if thread's event-loop never blocks anywhere spinning cpu @ 100% cpu usage. inefficient, since you'll wasting trillions of cpu cycles pointlessly spinning around event loop @ maximum speed while not doing work, , therefore cpu cycles unavailable use elsewhere (e.g. other programs or other threads in own program). generate excess heat , (on laptop or other portable device) drain battery quickly.
a better approach calculate when need select() wake (e.g. how time left between , next time need draw frame, or other scheduled task), , pass in amount of time select's timeout parameter. cause select()
block (for amount of time specified) , therefore allow thread sleep during period when doesn't need doing anything. make difference between program uses of (at least) 1 entire cpu core , 1 uses minimum amount of cpu time needs accomplish tasks.
Comments
Post a Comment