Archived

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

threads

This topic is 6276 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 am no expert on this as I have only just heard of threads myself, but I think threads are like functions. For example you can have your normal game loop running and then in parallel with that a thread can be calling a function to do some work. Infact it is not working in parallel but so fast that it seems that is. Take this diagram for example: start game loop | | V Get_controls(); ThreadFunction_checkObjects(); | | V Process_AI(); ThreadFunction_checkStates(); | | V Render_scene(); | | V sync. frames to 30 fps pr whatever | v end game loop this is a crude explaination of how threads work, but basically threads are like functions that can run independently of the game loop or path. In other words I think Windows calls then for you every so often.

Share this post


Link to post
Share on other sites
Thread creation is a manual process. There are design issues if you plan on using multiple threads. You have to make any data that might be accessed by 2 threads at the same time threadsafe... there are tons of books / tutorials on it. Good luck.

Share this post


Link to post
Share on other sites
SirCyr - Any place where you need to wait on something, typically I/O of some kind. There are other things, a physics engine maybe, or possibly even your AI. The other obvious spot is for Multiprocessor machines where you want to take advantage of all of the processors at once.

Share this post


Link to post
Share on other sites
To make a decision on whether or not you need threads in your game consider the following:

1.) Is there something I can do that can be done at the same time with minimal interference from my main thread.

A key example is networking.

It is extreamly inefficient to spin in a loop waiting for something to happen, now Winsock allows callbacks for some of its recv funtions, but it would be nice to have a thread that listens for data, and places it on a queue which could be read by other threads.

In your typical game, input from keyboard/mouse (1 thread), networking game loop (1 thread), and the main thread for your game/rendering loop (1 thread) = 3 threads.

But again most games only need 1 thread. Games that are designed to be multiplayer esp. with a client/server arch. Most of your threading will be in the server, the game clients should have < 3 threads.

To many threads can cause poor performance esp. if you have lots of shared data.


Here''s a typical multitreaded game loop
Each LOOP runs concerently with each other

Start control input thread
Start Network thread.
BEGIN GAME LOOP

Lock(input queue)
Does queue have data?
Yes-> process it
Unlock(input queue)


Lock(network queue)
Does queue have data?
Yes-> process it (read network messages)
Unlock(network queue)

Do your AI
Do some game logic (ie update items,players, states etc)
Render screen

END GAME LOOP

Concerently......

BEGIN INPUT LOOP

check for data in keyboard/mouse
if data then
Lock(input queue)
put data on queue
Unlock(input queue)
Yield thread

END INPUT LOOP

BEGIN Networking LOOP

Wait for data (timeout after X seconds)
Did we get data?
Yes-> Lock(networkqueue), add the data to the queue, Unlock(network queue)
Yield thread

END NETWORKING LOOP

It is important to yield your threads appropriately, i.e. if there''s nothing for them to do, give up their CPU time slice, so another thread can begin execution.
Make sure to lock/unlock any shared data or you can get all sorts of problems from deadlock to data corruption.


Share this post


Link to post
Share on other sites