Advertisement

Sockets Question

Started by August 14, 2003 06:49 PM
1 comment, last by WebsiteWill 21 years, 6 months ago
I think this was asked and answered before but the search button isn''t working for some reason so I''ll ask again. I currently have a thread that calls recvfrom on a blocking socket. If nothing is there to be received then the thread blocks and no CPU time is used up (a good thing). However, I am noticing that the thread is not exiting when the rest of the program does. The thread function is while loop while (bKeepThreadsAlive) { //Do stuff //call recvfrom() and block till info arrives } So, when I set bKeepThreadsAlive to false, my threads aren''t always shutting down. I''m pretty sure they shut down correctly if there is info to be received and the thread function can finish its processing. My send thread shuts down correctly but i only call send when I know there is information to be sent so it doesn''t block even though it is considered a blocking call. I''ve looked at non-blocking but polling a socket like that is supposed to eat up CPU cycles. Asynchronous is not an option (working on Linux). Select would probably eat up as much CPU as polling since it''s only dealing with a single socket. So any ideas on how I should handle this? I''m thinking that if I maybe set the socket to non-blocking then I can use Sleep() with a fitting time parameter to stop the polling from consuming too many CPUs. What do you guys think? Thanks, Webby
try making bKeepThreadsAlive volatile

Powered by Kentucky Fried EGGZ(r)
There are 10 kinds of people: those who understand binary code and those who don't.
Advertisement
closesocket(); it pulls the socket fd from under the sleeping thread, and recvfrom() falls over with an error code (EINTR). Works with both Windows and Unix.

-cb

[edited by - cbenoi1 on August 14, 2003 8:16:17 PM]

This topic is closed to new replies.

Advertisement