Sign in to follow this  
wilsq

multi-threaded structure

Recommended Posts

Hi, I've been writing a 2d game in vb.net using Managed DirectX Graphics. So far, the structure is something like this: Initial and load everything needed... While the game is running Update Inputs... Update objects... Render objects... Present the frame Loop Basiclly, every class involved inside the game loop has a method called Update. ie. the Update for a character would calculate the character's next position, and work out the which texture to use for the next step etc. After I read tons of articles on game programming, I decided to write the game in a multi-threaded way without the linear game-loop to maximize the performance. But I'm not sure how to do this in practice. Can anyone help?

Share this post


Link to post
Share on other sites
The setup isn't terribly different.

Each thread will run the same loop:

update inputs
update objects
render/play/send objects

loop.


threading just means that the inputs are more likely inter-thread communication rather than keystrikes, and the objects are likely a subset of the total objects rather than the entirety of the game.

Share this post


Link to post
Share on other sites
Thanks, but that's not exactly what i was thinking. Maybe multi-threading is not the correct word here since i don't know how non-liear structure is done. I was thinking about something like message-sending or event-driven.

Share this post


Link to post
Share on other sites
A thread is an independant path of execution in a program. Multi threading will do nothing to maximize performance in this case unless the system you are running on has more than one processor.

The purpose of using threads on a single processor system is to maintain responsiveness of other systems, such as the user interface. They are also used to do operations that may take a long time, but which other systems do not depend on. Reading in a large file that you don't need to access immediately is an example.

That's just FYI.


Now, if you want to use a message system, it's still going to be linear. "If it's not multi-threaded, it's linear."

The first thing you'll need is something to contain the messages. You'll also need to decide what the messages will need to contain. Your structure or class will probably contain fields like "command", "time sent", "from", "to", and "parameters."

Next you're going to need a centralized place for the messages to route through. Think of it as a post office. This class will be responsible for queuing messages from objects and delivering them to their recipients.

Now, do you want it to be a "push" system or a "pull" system?

Should objects interested in messages ask the post office "are there any messages for me?" or should each object have a "DeliverMessage" method that the post office calls when it wants to dispatch a message? Pretty much up to you. The second way is probably better performance-wise because there will be fewer function calls.

Also think about how you will identify objects. You'll need a unique ID number or name for each receipient in order to correctly deliver the messages.

Finally, what do you do with the messages once you get them? You'll need to fill in the details here because that's something I can't tell you.

Now, if you want a "true" event-driven system, then you'll want to look at the Observer pattern because that's essentially what you're talking about. One object registers with another and asks to be informed when something specific happens. This is done through function pointers: "Call this function when the event I'm interested in occurs, please." That is, unless, your language/platform provides events for you, like in java and C#.

Share this post


Link to post
Share on other sites
  1. If every object must be updated every frame (or at least on a regular basis), then a message system is not a good idea. A message system is appropriate when most of the objects need to be updated infrequently or because of an asynchronous event.
  2. Message systems are not cache-friendly. If you have a large group of similar objects that all receive the same message, it is better to instead have a single "manager" that receives the message and then updates the objects accordingly.

Share this post


Link to post
Share on other sites
Learn how to use a threading library. Then put your sub-systems on their own thread, providing that they arn't dependant on another sub-system. If sub systems are dependant on each other just group them together on the same thread.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this