threads

Started by
3 comments, last by Dark Star 23 years, 6 months ago
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.
---------------------------------------------You Only Live Once - Don't be afriad to take chances.
Advertisement
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.
Hi

I was wondering what was the real benefits of using threads in games?
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.
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.


This topic is closed to new replies.

Advertisement