Advertisement

non-blocking help

Started by December 21, 2003 01:16 PM
4 comments, last by Saucers 21 years, 1 month ago
hey all, i want to write a instant messenger style application to communicate over the internet (think MSN or AIM), and so obviously i dont want the program to freeze up with waiting sockets, so i want to use non-blocking sockets. Asynchronous is not an option as i want total portability to other platforms. However, whenever i use non-blockings, i get 99% CPU usage, which i really dislike, and is obviously not useful. How can i have non-blocking sockets that use relatively low CPU time in the program, yet still have a low in-program latency. if anyone has some practical socket resources, i would be very grateful. (also, if anyone has any ideas as to how threaded blocking sockets could do the job, i would be interested also). Cheers. -Saucers
Unless you explicitly tell your app to give up its share of CPU time, it will use all it can get. The problem you''re seeing has nothing to do with sockets or how you''re using them. Add Sleep(1) in your main loop and watch the results.
Advertisement
I''d probably go for the threaded blocking sockets. You could have one thread which waits until any open socket is readable. This would be done with the select function, which tells you exactly which sockets have data waiting. Another thread (even the main application thread) would then operate on the received data.
The exact structure of this will be up to you, but I''m sure you have some ideas.
thanks for the replies.

foofightr; how do third party msn apps on linux operate using such sockets when they clearly arent taking up relatively any CPU time, they look to be at 0% when idling, yet get any information sent to them apparently instantly. the same with the official client.

BeerHunter; i like the sound of this method, do you know if any other apps of this style use this technique? however, i would like to limit the number of sockets required (would this system require more open sockets?). do you have any threaded socket examples, because i have only fundimental socket experience, and next to no native threading experience. cheers.

-Saucers
Unfortunately, I only have minimal experience here.
I can point you to some information about it, but that''s about as much as I can do.
when a socket blocks it uses 0% cpu waiting for events to occur. So if your app is using 99% cpu it means that it''s polling, not blocking. If your using the select function try giving it a timeout value.
What this will do is cause your socket to block for X amount of time unless an event happens causing the function to return with the event that just happened. then you can process that event and return to blocking for more data. you should keep the timeout value small still so that you can continue to process any type of messeages the operating system is sending you. Or if you''re just using a text console then you might want it to be responsive for alowing text input or some other thing.

This topic is closed to new replies.

Advertisement