Jump to content
Posted 19 December 2012 - 03:09 PM
Posted 19 December 2012 - 08:32 PM
Posted 20 December 2012 - 12:40 PM
Edited by crancran, 20 December 2012 - 12:42 PM.
Posted 21 December 2012 - 12:26 PM
if you put your network connection into non-blocking mode, you can use select() and poll for data each loop and handle it accordingly
You can do that even with blocking sockets. That's the whole point of select(): When select() says a file descriptor is "ready" for reading or writing, it GUARANTEES that the next call to recv() or send() will not block, even if the socket is "blocking."
Posted 24 December 2012 - 05:15 PM
"yield thread" only yields until there is no other thread that wants the CPU, then you get the CPU back again.
You need to use a blocking primitive, like select() or sleep(), to actually reduce CPU usage. I recommend select(). Also, multiple threads is not recommended in the beginning of network programming. And, in fact, is often not needed at all.
I switched from using select to poll in a couple of programs a few months ago. The interface of poll is easier to work with.