Archived

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

Chimaera

Multi-threading

Recommended Posts

It might be asked on here a lot, but what do you think about multi-threading? When I think about it, I''ve never really heard much about it. Do people use it often (specifically for games) - and do they use it all around or just throw it in where it might come in handy? // chimaera

Share this post


Link to post
Share on other sites
for my 2cents.

It''s just a useful tool that might be handy to get you out of a jam.
You could useit for a lot of things but it does take quite a bit of working with to get it just right.

Yes, Its handy for networking and streaming. Or even running an evolution model in the background, but if you think about it there usally a way to get away without it.

If done badly it can lead to problems that could take you months {no joke}1 to find 1 line of code that frees a handle that makes your program crash irrationly.

Just my 2 cents.
Armand
1 if it''s in 3rd party stuff that your not expection to be buggy.


Share this post


Link to post
Share on other sites
Of course, not-with-specific-regard-to-game-programming,
multi-threaded / multi-process applications using ipc are
considered to be the best way to write robust long term
applications (from software developers I've talked to
anyway. =P)

The reason is fairly simple; a distinct program as part of a suite
is easier to produce as a milestone, less work, etc. than producing
one monolithic application that does everything.

Threading lets you get an easier job controlling and manipulating
each of the components.

There's a catch though: Threading is bad for time critical
applications, because exact time resolution of an event is
non-deterministic, because at any time the thread can be suspended
so other threads can run...

Which means for a game, having a thread can suck; because you can
time your core game loop and find 90% of the time it runs in 50ms.
And the rest of the time it runs 300ms because your stupid
[does something) thread is doing something.

It depends on the nature and goal of your programming task. You
have to remember; most computers only have one thread of control
active at any time, and although nice os's like 2000 have
scheduling, etc. there is still a tendency for cpu intensive
tasks (eg. undating game state every 50ms and rendering) to
steal that thread of control: making your other threads /
processes laggy.

& yeah. What armand said. Buggy libraries -really- suck.

[edited by - Shadow Mint on October 20, 2002 9:50:09 PM]

Share this post


Link to post
Share on other sites
Even for net-play you don''t need threads - you can just use the socket poll methods and periodically check for new data.

Once upon-a-time (I think it was over a year ago) we had an elaborate discussion about the circumstances that would make threaded games have higher performance that single-threaded ones. The first two things to off-load from the rendering loop is the physics and the AI.

The cost of syncronization is a concern, since all the threads would need to access the same geometric information. A createive method may be needed here (duplication of the geometry data, and use of an atomic syncronization method).

However, the magic that was required in order for this to have any chance of a performance benefit, is that the player would need a multi-processor machine. This would also mean the player would need to be running an OS based on the NT kernel, not that 9x crap.

Well, we now have XP for the home user (NT kernel), and the next-generation Pentiums have two instruction decoding units built into the chip - which appear to the OS as, and essentially function as two processors .

So the dawn of multi-threaded performance games is approaching (for the PC at least).

Right now, many PC games use threads for various task, such as networking. I wish more games would use threads or at least asyncronous techniques for level loading.

Share this post


Link to post
Share on other sites
To answer your original question... Yes, multi-threading is used extensively in professional games. But it''s a tool, like any other... it''s used to solve specific problems. You don''t just "throw it in" for the hell of it. But it''s something you''ll need to learn if you''re going to be a professional programmer.

Share this post


Link to post
Share on other sites
Ehh, what about implimenting threads for specific purposes.

The one that i''ve been considering it for, was that i can have a thread calculating the collision detection for this frame, one thread calculating the Occlusion culling, and mabey one thread calculating how sprites work.

you could pass a variable at the end of each thread to say "done" so that you can make sure the data is finished before the end of the frame.

Would that be as bad as you guys make it out to be?

~Main

==
Colt "MainRoach" McAnlis
Programmer
www.badheat.com/sinewave

Share this post


Link to post
Share on other sites
>The one that i''ve been considering it for, was that i can have
>a thread calculating the collision detection for this frame,
>one thread calculating the Occlusion culling, and mabey one
>thread calculating how sprites work.

>you could pass a variable at the end of each thread to
>say "done" so that you can make sure the data is finished
>before the end of the frame.

Yes you could do this. It would work.
What do you gain?

After you''ve created the threads,
Your main loop would then go off and at some point have to sit in a tight loop, with a system friendly sleep1
Waiting for the threads to finnish. This wait is unsessary if ou write it without threads.

You could go on ignoreing weather the threads have finnished but this would leed to instabillitys.

For doing what your thinking of, Physics and collision detection,
You might be better off useing a client server model.

This means you have effectivly 2 threads, but your not spawning them every frame. You start with them and you end with them.
They each handle their own stuff and update each other when needed.

>Would that be as bad as you guys make it out to be?

No but it will be slower.
Run it throught your head.
Try it.
Make your own desision2.
If it fails, fix it.

"The best compiler is between your ears" - Michael Abrash.

Armand.

1 say a sleep(1) or somthing but it has to hand control back to the os. Message pumping is also in this.
2 Okay so I''m 23 and my spelzing sucks.

Armand
-------------------------
It is a good day to code.

Share this post


Link to post
Share on other sites
From what I''m learning in my OS course, the purpose of having multi-threading/multi-processing is to minimize CPU idle time, or, in other words, maximum CPU time. So, if all the algorithms or functions which are CPU intensive, there''s no point in off loading them into a seperate thread, IN a SINGLE processor system. I don''t know how games would use threading, but OSes use threads for I/O stuff where you have to wait for stuff to happen.

Of course I haven''t finished the course yet, but this is what I''ve learned so far.

Share this post


Link to post
Share on other sites
quote:
Original post by Digicube
From what I''m learning in my OS course, the purpose of having multi-threading/multi-processing is to minimize CPU idle time, or, in other words, maximum CPU time.

But it is also to allow a sequential programming model rather than a polling one, allowing for greater modularity and therefore an improvement in potential software quality.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
One specific use of threads could be to load data that should be used later with otherwise unused processor cycles. I''ve heard that Diablo II used that to get smooth transition between maps.

Threading in games used for real-time stuff I don''t belive in (But that''s IMHO), because in games you generally want to get every frame in perfect sync. E.g. if you''re using on thread for physics and one for ai, that means that physics can at some point lag behind and the ai get faulty data..

-Luctus

Share this post


Link to post
Share on other sites