Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Blocking + threads vs. non-blocking

This topic is 5184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know questions very close to this one have been asked (I searched already) but I''m new to this whole network coding thing and most of the discussions go right over my head. Basically, I see two alternatives: * Use blocking sockets and multithreading to prevent application lockup * Use non-blocking sockets and check for stuff periodically. Now I don''t really know the advantages to one or the other, but there are a fe things: * single processor systems are the target. SMP might be nice, but I''m not writing for it and I''m not going to try to take any real advantage on it. * I''ve never actually written multithreaded code that had to deal with thread syncing, mutexes, etc. and so I''ll have to learn all of that if I use blocking sockets. I need to know what the major advantages are to either method. I''m looking for maybe 64 simultaneous connections max, so the abilities of the socket shouldn''t be a problem in either case. Whatever I do has to be portable, but I''m using SDL so I can take advantage of SDL''s thread libraries. Any answers and explanation would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Promit
Basically, I see two alternatives:
* Use blocking sockets and multithreading to prevent application lockup
* Use non-blocking sockets and check for stuff periodically.

* Use select.

Share this post


Link to post
Share on other sites
select allows you to block on multiple nonblocking sockets, so you don''t have to poll and you only need one thread.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
select allows you to block on multiple nonblocking sockets, so you don't have to poll and you only need one thread.


"Sneftel is correct, if rather vulgar." --Flarelocke


Wait...

so I can call select and optionally have it block a nonblocking socket? So are you saying I should use nonblocking sockets and select?

I'm confused...what exactly does select do? And what do I do to make a socket blocking or nonblocking, anyway?

[EDIT] Currently reading MSDN docs on select.

[edited by - Promit on March 4, 2004 8:37:21 PM]

Share this post


Link to post
Share on other sites
Actually, I kind of misspoke. Sockets by themselves are neither blocking nor nonblocking (when used with select). "select" can be used in blocking or nonblocking mode; in blocking mode, it blocks only until at least one socket in its set is ready to read/write. Thus, you can block on multiple sockets simultaneously.


"Sneftel is correct, if rather vulgar." --Flarelocke

[edited by - sneftel on March 4, 2004 8:51:35 PM]

Share this post


Link to post
Share on other sites
IIRC, with select blocking will depend on whether you set the fifth parameter (struct timeout) to 0 which means it will return right away regardless of state, greater than 0 is the amount of time (microseconds and milliseconds) it will wait for change in state before dropping out, and NULL will block until a file descriptor is ready.

Share this post


Link to post
Share on other sites
quote:
Original post by Nervo
IIRC, with select blocking will depend on whether you set the fifth parameter (struct timeout) to 0 which means it will return right away regardless of state, greater than 0 is the amount of time (microseconds and milliseconds) it will wait for change in state before dropping out, and NULL will block until a file descriptor is ready.


That doesn''t make sense; NULL and 0 are the same thing, they can''t behave differently...

Share this post


Link to post
Share on other sites
quote:
Original post by Promit
quote:
Original post by Nervo
IIRC, with select blocking will depend on whether you set the fifth parameter (struct timeout) to 0 which means it will return right away regardless of state, greater than 0 is the amount of time (microseconds and milliseconds) it will wait for change in state before dropping out, and NULL will block until a file descriptor is ready.


That doesn''t make sense; NULL and 0 are the same thing, they can''t behave differently...


Setting the members of the struct to zero will cause immediate drop-out. Setting the whole parameter to NULL will block. Sorry for the ambiguity.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!