More than one thread...

Started by
7 comments, last by Zipster 24 years ago
how would i create another thread from within my program and use it to do stuff, like loading data, specifically for games? lets see some code!! Edited by - Zipster on 4/1/00 5:27:03 PM
Advertisement
Look up CreateThread() in the SDK is the usual answer - but there was a pretty good tutorial on flipcode.com about this, I''m sorry but that''s all I can deliver this time of night.

Hope it helps,

-Mezz
thx, ill check it out
Don''t you think that creating a thread to load data would be rather pointless?
Things that require you to wait for something to finish processing before
you can do something else aren''t usually put into a thread. Threads are
meant to split up tasks which don''t need to wait for something else to finish.

Loading data is something you have to wait on, and I don''t think it is a
good candidate for threads. Mainly since you can''t do anything if you don''t
have the data in the first place.
Hi

It is yousefull, think of a game, that when you starts shows a little Animation, which is directly played from Disk, so for 5 Seconds, displaying PRoducer and Title of the Game. In this time you can load Data with an Thread, so that you can hit play in the Menu and the game starts without any (or much less) loading.


Lars
--------> http://www.larswolter.de <---------
quote:Loading data is something you have to wait on, and I don''t think it is a good candidate for threads.


Why isn''t it? for example, most games have some sort of a startup-menu or something, right? well, after the player clicks the new-game button, he would have to type in his name, chose his weapon, etc. (this is an example: and a very good one too, since most games don''t automatically start you playing when you click new-game until you type your name or give additional information). So, why wait for him to type his name in and chose his weapon and click Start, when you can load the first level while he is typing in that information?

Another example, some games have a little level complete screen after each level to show you your number of kills, score, etc.. This is the PERFECT place to load in the next level!!

So, you don''t always have to wait for the data to load if your doing something else first, and you KNOW that you''re going to need the data very soon! And, what if your in the middle of a level, and you have to load data for whatever reason (yes, in the middle of the level; don''t quote me on that one), why wait until the dire second the data is needed to make the player wait for it to load, when you can load it on another thread before the data is needed?

In all, there are many reasons to use multithreading to load data, and i think you just didn''t take the time to think of any examples before you replied. Also, when i said " for loading data ..." in my original post, that was just an example and not meant to be the ONLY reason to use multithreading, but i cleared up that argument, i think
Separate threads are a great place to put code that waits on some I/O device such as the network or the hard disk, the reason being that, while that thread is waiting for the device to send the data, your other threads can be processing game logic. Of course, there''s no point using an extra thread if you can''t continue at all without that data. Zipster gave some good examples of where you can tell what data is gonna be needed soon, without that data being needed immediately.
Just an implementation note (assuming Visual C++ here):

If you are using anything in the C runtime library (i.e. printf, fopen,...), you want to use _beginthread or _beginthreadex to create your threads. They perform some C-runtime stuff that needs to be handled so your new threads can use the C-Runtime library correctly and everything will be cleaned up properly.


-vince


-vince



i got it to work with multiple threads!!

all you have to do is create a UINT Thread (LPVOID lParam) function (this is where the code for the other thread goes; you need a different function for each thread), and then call AfxBeginThread(Thread, this); and you''re off!

This topic is closed to new replies.

Advertisement