multi-threading in multiplayer game code

Started by
3 comments, last by KingsRevenge 22 years, 8 months ago
What kind of a role would multi-threading have in MMORPG game code? How would it benefit the multiplayer code, what would be its PRO''s and Con''s? Eric Wright o0Programmer0o AcidRain Productions http://www.acidrainproductions.com
Eric Wright o0Programmer0o
Advertisement
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.
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
~ Tim
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
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

This topic is closed to new replies.

Advertisement