Multi-Threading help...

Started by
12 comments, last by GameDev.net 18 years, 6 months ago
Im working on the server side of my MMORPG engine and I'm finding it doesnt run as well as I have hoped so I figured if I mutli threaded each portion of my engine (AI, player control, map loading controls) it would run faster... Is it safe to mutli thread like this. Also anyone know of any good tutorals for multi-threading because I'm not to sure if im 100% sure how to do it...
Advertisement
If you're a beginner to multi-threading then you probably won't see a speed increase from multi-threading unless you run the program on a multi-processor, multi-core or hyperthreaded computer. And even then only maybe. So that's the first question to ask: is your server any of these? If not, you'd probably have more luck getting performance by optimizing your single threaded code.

Actually, have you profiled your code yet?
Im kinda starting clean with my network engine because I was having issues its all directx based... I will be running my server on a daul core 64bit amd system. The reason I thought mutli threading would work because what I am doing.. Every loop of the server I am checking and updating a list of over 5000 npc then checking and updating every character on the server (Which i hope for about 200+) and also one that will send out all the data such as maps and item info.

So all that in one frame can realy lag my system... I figured if I put each on in its own thread that it could calc each then send out all three in 1 lump sum... Or does it not make much of a difference?

Im not a complete begginer with threads but the onlything I currently have threaded is my messageproc... And searching around I cant find any materail on threading but I have also heard it not realy smart to thread.
Ive read alittle more.. Would multi threading work with loading a map in the background while the main game loop keeps running
Asynchronous loading is one the the easiest and most common uses of multithreading. It will definitely work for loading a map while your main loop runs.
Good see I have tought my self intirely by books most from the THOMSON: COURSE TECHNOLOGY series from Premier Press. And none of them realy mention multi threading. One shows you how to use it to for your messageproc and thats it. So I wasnt realy sure how exactly multi threading worked and what it was used for. Is it basicly just like running 2 or more programs at once and sharing infomation between the 2 but keeping it in one nice package.

And website or tutorals or even books on the subjects would be a nice help.
A easy way to think of multithreading is how many code paths are being executed at the current time. With your standard single threaded application you start at main and go from there. With multithreading you can start a thread anywhere you want.

Multithreading has many purposes but I use it for two main uses in my current engine.

1. Putting a proccessing loop on it's own thread. These threads live for the life of the process. I have separate threads for:

Rendering
Animation
Physics
Audio
AI
Input
Event Handling
Collisions

They're all working on the same scene graph at the same time but they're working on different aspects of it. The key to keeping multithreading simple is to keep each thread writing only one type of data. Once you have multiple threads writing to the same data then you have to worry about adding mutexes which means you have to worry about deadlocks and it becomes a pain.

2. Sequencing events

My event engine handles scene level, game level and system level events. If I have a complex event that's going to take a while to complete I don't want my event engine having to wait for it to complete. So instead I just spawn a thread for that complex event. These threads die after their action is complete.

Some people will say this many threads is overkill but I've found it's made things crazy easy. Some others will say that it's a performance problem since the OS is always switching between threads. That performance problem is all but non-existance in my experience. All modern OSes are fully multithreaded and you're already getting massive switching between your app and all the other system apps running. Adding a few more threads is nothing compared to what's already there if you don't believe me and are running Windows open up the task manager and add the threads column from the view menu. You will see that there are a few hundred threads running at any time on your machine.

I don't have any good book or tutorial suggestions for multithreading. I learned the basics in a class in Uni and learned the rest on my own. For some references go check out MSDN and search on CreateThread().
Thanks man that realy helps thats exactly what I was planning on doing with mine... I'll do it so each thread is treated like its own program so that I dont have to worry about variables and infomation being confused inbetween. Ill just work it as if I have a team of people and Ill keep one working ona porject at time... Thanks man thats the kinda of answer I realy needed.
Quote:Original post by LittleFreak
Thanks man that realy helps thats exactly what I was planning on doing with mine... I'll do it so each thread is treated like its own program so that I dont have to worry about variables and infomation being confused inbetween. Ill just work it as if I have a team of people and Ill keep one working ona porject at time... Thanks man thats the kinda of answer I realy needed.




They still arent independant. You have to protect shared data with interlocks to make sure that you dont try to read data that is half modified. It is simple when one thread is a consumer(reads) and one is a generator(reads/writes) as you can use a 'busy' flag (ie- like Critical Sections on Windows OS) that gets set only briefly on the buffer area containing the data anytime a read or a write happens. Even if you use a message passing scheme (to make requests or give commands) the link lists of messages still need an update flag while a message node is added or removed from a queue.

Do a data flow analysis of your app to find where/what the threads need to pass and the directions and amounts of data. See where you can put objectlike buffers (single buffer or queue) thru which data transfers will take place and each will have a Critical Section protecting access to it.


@Mike: i'd be very interested in details of how you were able to separate these components enough that threading becomes practical (especially since it would seem likely that Physics, AI, Input and Animation make changes to the Scene)

This topic is closed to new replies.

Advertisement