Jump to content
  • Advertisement
Sign in to follow this  
yahn

Threads or no threads?

This topic is 4261 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'm not sure if this should be in game programming or in network programming. It's asking more for advice on game programming then it is with network programming, so I posted here. I'm creating a socket dll for a game I'm making and I need the socket not to block while I'm waiting for it to receive. I already know how to use select(), but I'm thinking I should probably use a thread for this situation. I already have the server coded to use select. I thought it would be better for the server to use select since it will be dealing with a lot of sockets. I figure it would be better to use a thread for the client since they will only have one socket. What are your thoughts on weather I should use a separate thread or just use select()? And can you give a reason why you feel its better to use a thread or select()? If you think I should use a thread, do you know of any good place to learn how to use threads? Thank you for your time.

Share this post


Link to post
Share on other sites
Advertisement
select(), a thread per client is wasteful. I believe the FAQ has more details.

See question 10 in the networking faq.

http://www.gamedev.net/community/forums/showfaq.asp?forum_id=15

Share this post


Link to post
Share on other sites
Yes, but that seems to be referring to the server. I said that I have the server using select(), but I think that the client should maybe use a thread. I don't think question ten in the FAQ answered my question.

Share this post


Link to post
Share on other sites
It doesn't really make all that much difference when you're only dealing with 1 socket. Do whatever is easiest for you.

Share this post


Link to post
Share on other sites
In your client code, if you've got a game logic loop (which presumably you do), you may as well read the socket inside that loop, reading all pending messages each time, actioning them if necessary (of course depending on the network strategy this may involve queuing them for future use).

There seems to me to be no advantage in reading from the socket asynchronously (i.e. in another thread) when it only really affects your game logic loop anyway. It will just create synchronisation issues without affecting the performance of the game (much).

Mark

Share this post


Link to post
Share on other sites
In a client-server game the game logic loop is probably on the server, whereas the client would be running an event loop.

There are all sorts of problems you can run into trying to manage both GUI events and socket data.

Spinning the socket loop off to another thread is one approach. I went that way with my client and it works pretty well. The only hassle with it is needing to use mutexes.

Another approach is to set up an idle callback and read the socket in between GUI events. If you do that then you need to make sure your idle handler doesn't block the GUI.

A third approach is to try to turn your socket data into events. This doesn't seem to work well for many people, though. I tried it at first and dumped it in favour of the threaded approach, just ran into too many weird problems.

Which one is the right answer for you depends on your particular needs and problems, and what toolkits and OS you're developing with.

Share this post


Link to post
Share on other sites
I have my socket data handling code on a separate thread. Right now, I'm only using the queued data synchronously, but this way I have the flexibility to handle any traffic I want async, which is something I expect I'll be using in the future.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!