How does a thread work?

Started by
1 comment, last by Zemus 21 years, 6 months ago
I''ve heard about threads making programs able to do several things at the same time. Is this correct? And is there anyone that has a simple sample code on how to make threads?
Advertisement
The way theads work is simmilar to how you can multi-task, and they work in basicly the same way.

Just like you can run two programs at once, you can have one program run two things at once.

Unless you have a multi-processor system, they will not run at exactly the same time, rather windows will decide when each task can have a little bit of time to do its thing.

Generaly, you don''t need it in game programing _EXCEPT_ for network code, where you don''t want the networking part of your program sitting around waiting for info and leaving your program sitting around. Although there are some crazy things you can do with non-blocking sockets and DirectPlay, etc.

You will have to look up the code to do threads, it isn''t too hard to find. Start with looking up the function CreateThread() in the win32 API.

Do not meddle in the affairs of moderators, for they are subtle and quick to anger.ANDREW RUSSELL STUDIOS
Cool Links :: [ GD | TG | MS | NeHe | PA | SA | M&S | TA | LiT | H*R ]
Got Clue? :: [ Start Here! | Google | MSDN | GameDev.net Reference | OGL v D3D | File Formats | Go FAQ yourself ]

Since only 1 thread is actually running at the same time, switching back and forth quickly between threads, the gain that you get is when one of the threads is waiting on something like a hard disk operation which is VERY slow. If you have a place in your program where you are waiting a long time for things to be read in from disk then having another thread, that does something unrelated, that can go to work while the other is still waiting then you can get a lot more done. This dosn''t really happen that much in a video game. During load time sure but not during actual game play.

The code to get started with threads is not that complicated, but be aware that errors can occur that are next to impossible to figure out when you start using them. You can get bugs that are hard to track down because its the OS that schedules when a thread gets to run, so the order in which things are done can''t usually be duplicated.

I would say that you won''t need threads for any game you are writing, and that they might be more of a headache then they are worth.

This topic is closed to new replies.

Advertisement