Archived

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

multi-threading in multiplayer game code

This topic is 5968 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

Multithreading has the same tradeoffs in network programming as it does in any other situation. You give up simplicity in order to increase performance in a few key areas. Your question is a little too broad to give you a really good answer, and without a good grasp of how multithreading is commonly applied to network programming, the explanations wouldn''t really make much sense either.

If you want some good places to start, try looking into I/O Completion ports and Overlapped I/O as they relate to Winsock programming. The Completion port mechanism uses a thread-pool approach and it should give you some good ideas on the power of multithreading, and give you some more specific questions to ask about.

Share this post


Link to post
Share on other sites
Hi,

My experiance of threads in MMORPG games is new as i have just added into my one but basically i found out that without threads winsock will only alow upto something like 64 connections.. after that it blocks any more :-/ Also i believe it frees up processor time when a threaded tcp connection is lagging the rest of the threads and main server code continue to execute in the background :-)

Cons = implementing it :-p and you also need to be carfull about syncronisation... ie imaging if 2 threads are running "at the same time" and both read the same data.. then one writes back to it and then the other writes over what the 1st wrote .. that could be a big problem. basically u need to have a method of locking data while one thread is using it :-)

hope that helps

- Tim

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A MMRPG server basicly has to do three things: receive data from the clients, process data, and send data to the clients. A single-threaded server needs to perform those 3 steps one at a time, in sequence: receive, process, send. If there are many players connected to the server each step might take some time to complete and that might cause some problems. For instance, if the process and send steps take too long the input buffers might overflow and drop connections before the receive step can read the data (clearing the buffers). You will also get the undesirable effect that no data is sent to the clients during the receive and process steps, instead all the data from the server is pushed over the network during the send step, possibly choking the network.

Using multiple threads you can instead perform each of the steps in parallell, doing a little receiving, sending and processing all the time. A multi-threaded server might perform better since it can allow other threads to run if one thread should get locked in some non-processor dependent delay, such as waiting for I/O resources. Multiple threads are probably also necessary if you want your server to be scalable and use features such as multiple processors etc.. The downside to multi-threading is the increased complexity. Doing safe and efficient multi-threaded programming is HARD and the bugs that occur are usually extremely difficult to find and replicate.

Henry

Share this post


Link to post
Share on other sites
A good rule to live by is to keep each additional thread performing a very modular activity. This will protect you from headaches and help avoid excessive wait time for multiple resource objects.

Also keep your thread count down. Multithreading is only beneficial until the point that frequent context switching begins to eat away at your execution time. Multithreading is most advantageous if you have a system with multiple processors, as context switches are less burdensome.

Tim/Fingh

Share this post


Link to post
Share on other sites