Is it possible ?

Started by
7 comments, last by veNa 19 years, 11 months ago
Is it possible for me to program my program to do two things at the same time ? i mean in parallel... I''m kinda newbie to the game developing industry and to the programming industry at all, but from what i''ve seen so far, people are programming their programs to do one thing at a time... then another thing, and another and so on and so forth like so until they finish. Thanks a lot for you time, Arie. veNa - Born to make games.
veNa - Born to make games.
Advertisement
Well, technically, it isn''t possible because the CPU only does one thing at a time. However, it is possible in the same sense that it is possible to have two programs run at the same time. There are two ways to do it, in fact.

1- Just have both tasks execute one after the other in your game loop. If it''s something simple enough (like moving two sprites at once, for instance) then this is the best solution. Every update both actions will have occured.

2- Create a thread. Threads are often over-used, though. Using a thread could be good if, say, you have music playing in the background. A thread is to a program like a program is to an OS. That is, your program manages a certain number of threads just like the OS manages a certain number of programs. If that makes sense...
Short answer: on a computer with only one CPU its physically impossible to run different bits of your program at the same time.

Long answer: Just like how cartoons and film fake movement by lots of still images spliced together at high speed, operating systems constantly switch from running one program to another to give the illusion that they''re running at the same time. Which is why I''m able to listen to music from winamp while browsing the ''net.

Doing multiple tasks in parallel within a single program means you need to work in multiple ''threads''. You can imagine it as being two (or more) running programs that happen to share the same memory.

However, if you''re writing a game then multiple threads are often not needed much (maybe one for networking or sound). *Don''t* try to make a separate thread for each different moving object in your game, just process them all for one time slice and update the screen. Wash, rinse, repeat.
Usually you can only do one thing at a time, but you can do two or more things interleaved. The computer switches between two tasks very rapidly, to a human it will seem as though the two tasks happen simultaneously.

There are multiple ways you can accomplish this in a program:
- Do it your self, this is what most games do. In the main loop they update the position of all objects (one at a time), they appear to be moving at the same time.
- Use multithreading. You split up the program in multiple threads, the operating system will handle the execution of these threads. This means that you have less control over what happens exactly.
quote:Short answer: on a computer with only one CPU its physically impossible to run different bits of your program at the same time.

quote:Well, technically, it isn''t possible because the CPU only does one thing at a time.


Well with the new Hyper Threading technology....I think it is possible to have the cpu do more thatn one thing at a time.

"A soldier is a part of the 1% of the population that keeps the other 99% free" - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA
quote:Original post by ncsu121978
quote:Short answer: on a computer with only one CPU its physically impossible to run different bits of your program at the same time.

quote:Well, technically, it isn''t possible because the CPU only does one thing at a time.


Well with the new Hyper Threading technology....I think it is possible to have the cpu do more thatn one thing at a time.


Yes, because an HT processor acts as two separate logical units, each with their own set of registers.
Don''t confuse the poor guy.
why would it be nessesary to make a seperate thread for music? it doesnt make sence... in my games it was easy to manage music..(using SDL_mixer)
FTA, my 2D futuristic action MMORPG
quote:Original post by graveyard filla
why would it be nessesary to make a seperate thread for music? it doesnt make sence... in my games it was easy to manage music..(using SDL_mixer)

I don''t know the internal workings of SDL_mixer, but i''m guessing that it creates a music thread without you knowing it.

This topic is closed to new replies.

Advertisement