Question about Threads and GM

Posted by Glen on Jan. 11, 2011, 4 p.m.

To my understanding, threads speed up processing by splitting the processes. For example, if I want something done without it lagging the game, I can have it processed outside of gm as if it were a separate thread? And if that's the case, does this mean doing everything in their own threads will speed up things?

Situation:

It takes 4 seconds for 60mb worth of external images to be unpacked (just a folder with 300 random images for testing), and then repacked back to it's compressed state. (Sorta like a zipped package of resources). Would doing the unpacking/packing process in it's own thread speed up the process? Or will it just eliminate lag on the screen?

I'd like to learn more about threads. I've been interested in this topic for awhile and have tried numerous times to mess with them but not doing so well.

Comments

aeron 13 years, 10 months ago

Quote:
Would doing the unpacking/packing process in it's own thread speed up the process? Or will it just eliminate lag on the screen?
Well it would really only eliminate lag on the screen. In essence the two threads are allowed to run in parallel but it's not like either one will run faster, they just run at their own separate paces.

Quote: Laurent Gomila (sfml docs)
First, what is a thread ? What should you use it for ?

Basically, a thread is a way to run a set of instructions independently of the main thread (every program runs in a thread : it is a monothreaded program ; when you add one or more threads, it is called a multithreaded program). It is just like running another process, except that it is much lighter – threads are also called lightweight processes. All threads share the same datas as their parent process. As a result, you can have two or more sets of instructions running in parallel within your program.

Ok, so when should you use threads ? Basically, when you have to run something that would take long (complex calculations, waiting for something, …), and you still want to do something else at the same time. For example, you may want to be able to display a graphical interface while you load your game resources (that takes usually a long time), so you can put all the loading code into a separate thread. It is also widely used in network programming, to wait for network data to be received while continuing running the program.

Grand-High Gamer 13 years, 10 months ago

Not even seperate paces, it's just that the CPU will flip flop between the two so that both appear to happen concurrently. Best not to think of "making things faster" so much as "running things in the background".

PY 13 years, 10 months ago

If you had to unpack, process, and repack, then threads could really help by letting you process at the same time as you're unpacking others. It lets you run more than one thing in parallel, but your algorithm has to be able to split things into parallel parts that don't rely on each other to run. For example, you could split, say, physics from objects that aren't interacting into different threads, but you couldn't put physics from two colliding objects in different threads because they rely on information from each other, which they obviously don't have because the other may or may not have been processed yet.

Most tasks can't be split into threads like that, but if they can you can get good performance boosts. It isn't an easy performance boost, by any means, it requires a fundamentally different train of thought to design a threaded game engine than a non-threaded engine.

Grand-High seems to be stuck back in the 20th century, where multi-core processors weren't commonplace and true concurrency was impossible. This is no longer the case.

Glen 13 years, 10 months ago

My main reason for external resources is initial loading time. Most players won't see half the content in the game unless they play it to the fullest. I feel that loading only the resources needed (as you get further into the game) is just common sense. Plus editing content without forcing people to redownload everything makes patching easy. Especially if you have a big project with lots of images/music that take up space. No one wants to download 60mb every time there's an update, lol. But even if I do only load a little bit at a time throughout the game, the idea of using threads to make it more efficient and getting rid of the split second pause on the GM window is attractive to me.

If I wanted to use this: http://gmc.yoyogames.com/index.php?showtopic=390517 would it do what I'm looking for? Have any of you tried using this DLL?

Cesque 13 years, 10 months ago

Quote:
Grand-High seems to be stuck back in the 20th century, where multi-core processors weren't commonplace and true concurrency was impossible.

Horrible times. I heard they also had had Ford T's and Hitler back then.

Glen 13 years, 10 months ago

I drive a 96' Ford Thunderbird….