Jump to content
  • Advertisement
Sign in to follow this  

Multi-Threading help...

This topic is 4800 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 believe the first thing to do is profile your code and find where is the performance hit. One way of improving your 5000 npc code is to have a list of "active" npc, that are close enough to a player so that there could be interaction, and loop into them every cycle.

And every 10 or 20 cycle, you can go through your whole list, to update your "active" npc list. This way, you can improve this update time a lot!

Back to multithreading: I personnaly use 3 threads in my MORPG:
- The main thread
- The network thread, that receives and sends packets to players
- The data thread, that provides asyncronous access to the database.

By using queues to transfer messages between these threads, I reduce to the max the risks of each thread waiting for another, and ensure that no deadlock is ever possible.

Share this post

Link to post
Share on other sites
Original post by LittleFreak
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.

Tutorials for what? How to design/create multithreaded applications, or do you need a syntax for multithreading in your language?

If you need to look at the thread problematic, try to google for Java + multithreading + tutorial. Or Java + Threads + problems. (Java has crossplatform multithreading defined in the standard library, so it's easy to find demonstration programs, or articles about multithreading.)

Also look at IBM alphaworks. IIRC there were articles with a nice describtions of threads, lock problems, and other multithreaded features (Some of them were using C++, others Java. You'd need to look at both.).

And one big warning. Before you'd add a multithreading support into you engine, create a few testing programs, OR YOU'D MESS YOUR ENGINE UP. Multithreaded programs needs a lot of experience to write, and experience is done by testing, writing, screaming, and then finally using brain, then writing a new version, then testing again.

A few important links

A nice tutorial about threads with demonstrations. (Download update from java.com if your computer doesn't have Java 5.0 compatibile libraries installed. It works much better with JVM younger than 3 years.)

Another keywords you might like to look at are. Race condition. Deadlock. Out of order execution. Diference in behavior of multithreaded programs on single core, and multi core CPU.

You have dual core so majority of problems would be visible, of course with exception of problems that are visible only on a dual CPU computer, or computers with 250 CPUs.

Of course server for MMORPG has a completely different structure and requirements than a single user application. Can you deal with the packet storms? Are you compressing data before send? What is biggest latency you could afford? Do you know MUDs, and have you educated from theirs source code? (However, I don't know if there was a MUD with multithreaded support.) These are questions you should already have answers, at least partial, before you would finish first version of your server.

Yes and one more advice, don't try to lock too much, or you'd change your multithreaded program to in effect nearly single threaded with a lot of lock overhead.

Share this post

Link to post
Share on other sites
Thanks guys I figured it out and I think I got the right idea also I'm picking up a new book I saw suggested that has a part in it about when exactly to and when not to use it in a program and well I think Im good for now thanks for the help.... And for a laugh this is the test program I wrote to teach myself... Its amusing since I wanted to keep it simple and just use a console app with iostream.

#include <windows.h>
#include <iostream>

using namespace std;

int i;
for(i=0; i<101; i++)
cout << i << "\a";

return 0;


int main()
HANDLE hTread;
DWORD idTread;
int num1;
int num2;

hTread = CreateThread(NULL, 0, Count, 0, 0, &idTread);

cout << "Enter first number ";
cin >> num1;
cout << "Enter second number ";
cin >> num2;

cout << " Num1 + Num2 = " << num1 + num2 << endl;


return 0;

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Hints and tips for multithreading.

1/ Only have one thread reading the windows message pump all other threads are worker.
2/ Communicate between threads usings blocking protected read/write Queues or atomic flags (don't use the windows message Q!)
3/ Update the screen with whats changed using both OnIdle or OnTimer (delay configurable) events.

Use waitformultipleevents so you can wait for a long time for an event or the cancel event to occur.
Check if the thread has been cancelled after waitfor events.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!